Configuration、SessionFactory、Session

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获得 Sessioninstances 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 INSERTdelete() 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 UPDATEsaveOrUpdate() 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.

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

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值