Configuration、SessionFactory、Session、Transaction

本文介绍了Hibernate中Configuration类的作用及使用方式,包括如何通过Configuration创建SessionFactory实例,并概述了SessionFactory和Session接口的主要职责。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 
public class Configuration
extends Object implements Serializable

An instance ofConfigurationallows the application to specify properties and mapping documents to be used when creating aSessionFactory. Usually an application will create a singleConfiguration, build a single instance ofSessionFactoryand then instantiate(实例化)Sessions in threads servicing client requests. TheConfigurationis meant only as an initialization-time object.SessionFactorys are immutable(不可变的) and do not retain(保留) any association(关联) back to the Configuration.

A newConfigurationwill use the properties specified inhibernate.propertiesby default.

buildSessionFactory

public SessionFactory buildSessionFactory()
                                   throws HibernateException
Instantiate a new SessionFactory, using the properties and mappings in this configuration. The SessionFactorywill be immutable, so changes made to the Configurationafter building the SessionFactorywill 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(创建) ofSessioninstances. Usually an application has a singleSessionFactoryinstance and threads servicing client requests obtain(获得)Sessioninstances from this factory.

The internal(内部) state(状态) of aSessionFactoryis immutable. Once it is created this internal state is set. This internal state includes all of the metadata(元数据) about Object/Relational(关系) Mapping.

Implementorsmustbe 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 aSessionis bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)

The main function of theSessionis 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 anySession
persistent:associated with a uniqueSession
detached:previously persistent, not associated with anySession

Transient instances may be made persistent by callingsave(),persist()orsaveOrUpdate(). Persistent instances may be made transient by callingdelete(). Any instance returned by aget()orload()method is persistent. Detached instances may be made persistent by callingupdate(),saveOrUpdate(),lock()orreplicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by callingmerge().

save()andpersist()result in an SQLINSERT,delete()in an SQLDELETEandupdate()ormerge()in an SQLUPDATE. Changes topersistentinstances are detected at flush time and also result in an SQLUPDATE.saveOrUpdate()andreplicate()result in either anINSERTor anUPDATE.

It is not intended(意味着) that implementors be threadsafe. Instead each thread/transaction should obtain(获得) its own instance from aSessionFactory.

ASessioninstance 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 particularSession at any time.

Implementors are not intended to be threadsafe.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值