org.hibernate.cfg
Class Configuration
An instance of Configuration配置 allows theapplication应用 tospecify指定 properties性能 andmapping映射 documents to be used when creating a SessionFactory. Usually an application will create a single Configuration,build构造 a single instance of SessionFactory and then instantiate Sessions in threads servicingclient客户 requests. The Configuration is meant only as an initialization-time object. SessionFactorys areimmutable不变的 and do notretain保持 any association back to the Configuration.
A new Configuration will use the properties specified in hibernate.properties by default.
buildSessionFactory
Instantiate举例说明 a using new SessionFactory, the properties and mappings in this configuration. The SessionFactory will be immutable, so changes made to the Configurationafter building the SessionFactory will not affect it.
org.hibernate
Interface SessionFactory
The main contract here is the creation of Session
instances. Usually an application应用 has a single SessionFactory
instance and threads servicing client requests obtain获得 Session
instances from this factory.
The internal内部 state of a SessionFactory
is immutable. Once一旦 it is created this internal state is set设置. This internal state includes all of the metadata元数据 about Object/Relational Mapping.
Implementors must be threadsafe.
org.hibernate
Interface Session
The mainruntime运行时 interface between a Java application and Hibernate. This is the central API classabstracting摘要 thenotion概念 of apersistence持续 service.
The lifecycle生命周期 of a Session is bounded有界限 by the beginning and end of alogical合理的 transaction事务. (Long transactions might spanseveral几个 database .)
The main function of the Session is 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 any Session
persistent持久的 : associated with a unique Session
detached分离的: previously在前 persistent, not associated with any Session
Transient instances may be made persistent by calling save(), persist() or saveOrUpdate(). Persistent instances may be made transient by calling delete(). Any instance returned by a get() or load() method is persistent. Detached instances may be made persistent by calling update(), saveOrUpdate(), lock() or replicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge().
save() and persist() result in an SQL INSERT, delete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances aredetected发现 at flush time and also result in an SQL UPDATE. saveOrUpdate() and replicate() result in either an INSERT or an UPDATE.
It is not intended准备 that implementors实现 be threadsafe.Instead反而 each thread/transaction事务 shouldobtain获得 its own instance from a SessionFactory.
A Session instance 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发生 .
org.hibernate
Interface Transaction
A transaction is associated关联 with a Session and is usually instantiated by a call to Session.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 particular特别 Session at any time.
Implementors are not intended to be threadsafe.