public class Configurationextends Object implements Serializable
An instance ofConfigurationallows the application to specify properties and mapping documents to be used when creating aSessionFactory. Usually an application will create a singleConfiguration, build a single instance ofSessionFactoryand then instantiate(实例化)Sessions in threads servicing client requests. TheConfigurationis meant only as an initialization-time object.SessionFactorys are immutable(不可变的) and do not retain(保留) any association(关联) back to the Configuration.
A newConfigurationwill use the properties specified inhibernate.propertiesby default.
buildSessionFactory
public SessionFactory buildSessionFactory() throws HibernateException
- Instantiate a new SessionFactory, using the properties and mappings in this configuration. The SessionFactorywill be immutable, so changes made to the Configurationafter building the SessionFactorywill not affect it.
-
-
Returns:
- a new factory for Sessions Throws:
-
HibernateException
See Also:
-
SessionFactory
public interface SessionFactoryextends Referenceable, Serializable
The main contract(作用) here is the creation(创建) ofSession
instances. Usually an application has a singleSessionFactory
instance and threads servicing client requests obtain(获得)Session
instances from this factory.
The internal(内部) state(状态) of aSessionFactory
is immutable. Once it is created this internal state is set. This internal state includes all of the metadata(元数据) about Object/Relational(关系) Mapping.
Implementorsmustbe threadsafe.
public interface Sessionextends Serializable
The main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service.
The lifecycle of aSessionis bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)
The main function of theSessionis to offer create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:
transient:never persistent, not associated with anySession
persistent:associated with a uniqueSession
detached:previously persistent, not associated with anySession
Transient instances may be made persistent by callingsave(),persist()orsaveOrUpdate(). Persistent instances may be made transient by callingdelete(). Any instance returned by aget()orload()method is persistent. Detached instances may be made persistent by callingupdate(),saveOrUpdate(),lock()orreplicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by callingmerge().
save()andpersist()result in an SQLINSERT,delete()in an SQLDELETEandupdate()ormerge()in an SQLUPDATE. Changes topersistentinstances are detected at flush time and also result in an SQLUPDATE.saveOrUpdate()andreplicate()result in either anINSERTor anUPDATE.
It is not intended(意味着) that implementors be threadsafe. Instead each thread/transaction should obtain(获得) its own instance from aSessionFactory.
ASessioninstance is serializable if its persistent classes are serializable.
A typical transaction should use the following idiom:
Session sess = factory.openSession(); Transaction tx; try { tx = sess.beginTransaction(); //do some work ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); }
If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs(发生).
public interface Transaction
Allows the application to define units of work(操作单元), while maintaining(维护) abstraction from the underlying(基础) transaction implementation (实现)(eg. JTA, JDBC).
A transaction is associated with a Session and is usually instantiated by a call toSession.beginTransaction(). A single session might span multiple transactions since the notion of a session (a conversation(通话) between the application and the datastore) is of coarser granularity than the notion of a transaction. However, it is intended(意味着) that there be at most one uncommitted Transaction associated with a particularSession at any time.
Implementors are not intended to be threadsafe.