读书人

Hibernate Core Reference Manual学习

发布时间: 2013-01-02 13:08:44 作者: rapoo

Hibernate Core Reference Manual学习笔记——Chapter 2. Architecture

Chapter 2. Architecture


Table of Contents
2.1. Overview
2.1.1. Minimal architecture
2.1.2. Comprehensive architecture
2.1.3. Basic APIs
2.2. JMX Integration
2.3. Contextual sessions

2.1. Overview
站在很高的层次看Hibernate,它是下面这个样子:

Hibernate Core Reference Manual学习札记——Chapter 2. Architecture
?
由于Hibernate架构十分的灵活,所以我们不可能提供所有可能的运行时架构。这里只介绍介绍两种特殊情况:
2.1.1. Minimal architecture
最简架构:应用程序自己管理它的JDBC连接,并把这些连接提供给Hibernate。同时,应用程序自己管理transaction。这种方式使用了Hibernate API的一个最小子集。
Hibernate Core Reference Manual学习札记——Chapter 2. Architecture
?
2.1.2. Comprehensive architecture
"comprehensive"架构使应用程序从底层的JDBC和JTA API抽象出来,并让Hibernate来管理这些细节。

Hibernate Core Reference Manual学习札记——Chapter 2. Architecture
2.1.3. Basic APIs

?

A thread-safe, immutable不可变的 cache of compiled mappings for a singledatabase. A factory for?org.hibernate.Session?instances. A client oforg.hibernate.connection.ConnectionProvider. Optionally maintains a?second level cache?of data that is reusable betweentransactions at a process or cluster 集群level.

A single-threaded, short-lived objectrepresenting a conversation between the application and the persistent store.Wraps a JDBC?java.sql.Connection. Factory fororg.hibernate.Transaction. Maintains a?first level cache?of persistent the application's persistentobjects and collections; this cache is used when navigating the object graph orlooking up objects by identifier.

Short-lived, single threaded objectscontaining persistent state and business function. These can be ordinaryJavaBeans/POJOs. They are associated with exactly one?org.hibernate.Session. Once the?org.hibernate.Session?is closed, they will be detached and freeto use in any application layer (for example, directly as data transfer objectsto and from presentation).?Chapter?11,?Working with objects?discusses transient, persistent anddetached object states.

Instances of persistent classes that arenot currently associated with a?org.hibernate.Session. They may have been instantiated by the application andnot yet persisted, orthey may have been instantiated by a closed?org.hibernate.Session.?Chapter?11,?Working with objects?discusses transient, persistent anddetached object states.

(Optional) A single-threaded, short-livedobject used by the application to specify atomic units of work. It abstractsthe application from the underlying JDBC, JTA or CORBA transaction. A?org.hibernate.Session?might span several?org.hibernate.Transactions in some cases. However, transactiondemarcation, either using the underlying API or?org.hibernate.Transaction, is never optional.

(Optional) A factory for, and pool of,JDBC connections. It abstracts the application from underlying?javax.sql.DataSource?or?java.sql.DriverManager. It is not exposed to application, but itcan be extended and/or implemented by the developer.

(Optional) A factory for?org.hibernate.Transaction?instances. It is not exposed to theapplication, but it can be extended and/or implemented by the developer.


使用Hibernate 3.0(及以前)的应用程序通常会利用ThreadLocal-based contextual sessions和辅助类HibernateUtil,或者是利用第三方的框架,例如Spring和Pico,他们可以提供proxy/interception-based contextual sessions.


从Hibernate 3.0.1开始,Hibernate添加了 SessionFactory.getCurrentSession()方法。起初,这个方法假定使用JTA transactions,而JTA transaction定义当前session的scope和context。考虑到很多独立的JTA TransactionManager实现都很成熟,在绝大多数情况下,应用程序应该使用JTA transaction management。不论应用程序是否部署到J2EE容器中去。基于此,the JTA-based contextual sessions are all you need to use.


但是到了Hibernate 3.1,SessionFactory.getCurrentSession()底层的处理变成了可插拔的。为了实现这一目标,一个新的接口org.hibernate.context.spi.CurrentSessionContext和一个新的配置参数 hibernate.current_session_context_class被添加进来。这就允许你以可插拔的方式配置当前session的scope和context。

查看 org.hibernate.context.spi.CurrentSessionContext接口的Javadocs.可以看到它仅定义了一个方法currentSession()。它的实现类负责追踪current contextual session。同时Hibernate提供了对该接口的三种实现:

读书人网 >软件架构设计

热点推荐