Configuration、SessionFactory、Session、Transaction

public class Configuration
extends Object
implements Serializable

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 SessionFactory and 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.

buildSessionFactory

public SessionFactory buildSessionFactory()
                                   throws HibernateException
Instantiate a new SessionFactory, using the properties and mappings in this configuration. The SessionFactory will be immutable, so changes made to the Configuration after building the SessionFactory will not affect it.

Returns:
a new factory for Sessions
Throws:
HibernateException
See Also:
SessionFactory


public interface SessionFactory
extends Referenceable, Serializable

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.



public interface Session
extends 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 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(发生).

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 particular Session at any time.

Implementors are not intended to be threadsafe.


posted @ 2012-04-17 17:14  hibernate3例子  阅读(255)  评论(0编辑  收藏  举报