hibernate 翻译

Configuration

An instance of Configuration allows(允许) the application to specify properties and mapping documents to be used when creating a SessionFactory. Usually(通常) an application will create a single Configuration, build a single instance of SessionFactoryand then instantiate Sessions in threads servicing client requests. The Configuration is meant(意味) only as an initialization(初始化)-time object. SessionFactorys are immutable(不可变) and do not retain(保留) any association(协会) back to the Configuration.

A new Configuration will use the properties specified in hibernate.properties by default.

NOTE : This will be replaced(取代) by use of ServiceRegistryBuilder and MetadataSources instead(替换) after the 4.0 release(发布) at which point this class will become deprecated and scheduled(预期) for removal in 5.0. See HHH-6183HHH-2578 and HHH-6586 for details

 

 

buildSessionFactory

Create a SessionFactory using the properties and mappings in this configuration. The SessionFactory will be immutable(不可变), so changes made to thisConfiguration after building the SessionFactory will not affect(影响) it.

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.

Session

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 a Session is bounded(有界限的) by the beginning and end of a logical transaction. (Long transactions might(可能) 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() orreplicate(). 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 INSERTdelete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances are detected at flush time and also result in an SQL UPDATEsaveOrUpdate() and replicate() result in either 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.

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.

猜你喜欢

转载自ears.iteye.com/blog/1488619
今日推荐