org.hibernate.cfgClass Configuration
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 instantiateSessions in threads servicing client requests. TheConfigurationis meant(意味着) only as an initialization-time object.SessionFactorys are immutable anddo not retain(不保留) anyassociation(关联) back to theConfiguration.
A newConfigurationwill use the properties specified inhibernate.propertiesby default.
buildSessionFactory
Instantiate a newSessionFactory, using the properties and mappings in this configuration. TheSessionFactorywill be immutable, so changes made to theConfigurationafter building theSessionFactorywill notaffect(影响) it.
org.hibernate
Interface SessionFactory
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 isset(设置的). This internal state includes all of themetadata(元数据)about Object/Relational Mapping.
Implementorsmustbe threadsafe.
org.hibernate
Interface Session
The main runtimeinterfacebetween a Java application and Hibernate. This is the central API class abstracting the notion(概念) of a persistence(持久化) service.
Thelifecycle(生命周期) 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 atransient
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 aredetected(检测到)
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 shouldobtain(获得,获取) 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 theSessionthrows an exception, the transaction must be rolled back and the sessiondiscarded(废弃,丢弃). The internal state of theSessionmight not be consistent(不一致) with the database after the exceptionoccurs(发生).
org.hibernate
Interface Transaction
A transaction is associated(关联) with aSessionand 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 uncommittedTransactionassociated with a particularSessionat any time.
Implementors are not intended to be threadsafe.
本文深入探讨了Hibernate框架中Configuration和SessionFactory的概念,解释了如何通过Configuration指定属性和映射文档来创建SessionFactory。重点阐述了Configuration作为初始化时的对象,SessionFactory的不可变性及其与Configuration的关联。同时介绍了Session作为Java应用与Hibernate之间的主要接口,以及其生命周期和状态变化的基本流程。通过典型事务使用模式的示例,展示了如何在实际应用中正确地管理Session和事务。
407

被折叠的 条评论
为什么被折叠?



