public class Configuration
An instance of Configuration allows(允许) the application tospecify(指定) properties and mapping 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 servicing client requests. The Configuration is meantonly(只有) as aninitialization(初始化)-timeobject. SessionFactorys are immutable(不可变的) and do notretain(保留) anyassociation(关联) back to the Configuration.
A new Configuration will use the properties specified(详细说明) in hibernate.properties(属性) by default.
public SessionFactory buildSessionFactory() throws HibernateException
Instantiate(实例化) a new SessionFactory, using theproperties(属性) andmappings(映射) in thisconfiguration(配置). The SessionFactory will beimmutable(不可变的), so changes made to the Configurationafterbuilding(增加) the SessionFactory will notaffect(影响) it.
HibernateException
SessionFactory
The main contract(合同) here is the creation(创造) of Session
instances. Usually an application has a single(一个)SessionFactory
instance and threads servicing client requestsobtain(获得)Session
instances from this factory.
The internal(内部的) state of a SessionFactory
isimmutable(不可变的).Once it is(一旦) created this internal state is set. This internal state includes all of themetadata(元数据)about(关于) Object/Relational(关系) Mapping.
Implementors must be threadsafe.
public interface Session
The main(主要的) runtime interface between a Java application and Hibernate. This is thecentral(集中的) API classabstracting(抽象) thenotion(概念) of a persistence(持久性) service.
The lifecycle(生命周期) of a Session isbounded(有界限的) by the beginning and end of alogical(逻辑) transaction. (Long transactionsmight(可能) span(跨越)several(几个的) database transactions.)
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 ineither(任何一个) an INSERT or an UPDATE.
It is not intended(打算) that implementors be threadsafe.Instead(反而) each thread/transaction should(应该)obtain(获得) 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(出现).