Hibernate体系结构
来源:互联网 发布:程序员背包 编辑:程序博客网 时间:2024/04/20 19:36
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. Contextual sessions
2.1. Overview
The diagram below provides a high-level view of the Hibernate architecture:
Unfortunately we cannot provide a detailed view of all possible runtime architectures. Hibernate is sufficiently flexible to be used in a number of ways in many, many architectures. We will, however, illustrate 2 specifically since they are extremes.
2.1.1. Minimal architecture
The "minimal" architecture has the application manage its own JDBC connections and provide those connections to Hibernate; additionally the application manages transactions for itself. This approach uses a minimal subset of Hibernate APIs.
2.1.2. Comprehensive architecture
The "comprehensive" architecture abstracts the application away from the underlying JDBC/JTA APIs and allows Hibernate to manage the details.
2.1.3. Basic APIs
Here are quick discussions about some of the API objects depicted in the preceding diagrams (you will see them again in more detail in later chapters).
- SessionFactory (
org.hibernate.SessionFactory
) A thread-safe, immutable cache of compiled mappings for a single database. A factory for
org.hibernate.Session
instances. A client oforg.hibernate.connection.ConnectionProvider
. Optionally maintains asecond level cache
of data that is reusable between transactions at a process or cluster level.- Session (
org.hibernate.Session
) A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC
java.sql.Connection
. Factory fororg.hibernate.Transaction
. Maintains afirst level cache
of persistent the application's persistent objects and collections; this cache is used when navigating the object graph or looking up objects by identifier.- Persistent objects and collections
Short-lived, single threaded objects containing persistent state and business function. These can be ordinary JavaBeans/POJOs. They are associated with exactly one
org.hibernate.Session
. Once theorg.hibernate.Session
is closed, they will be detached and free to use in any application layer (for example, directly as data transfer objects to and from presentation). Chapter 11, Working with objects discusses transient, persistent and detached object states.- Transient and detached objects and collections
Instances of persistent classes that are not currently associated with a
org.hibernate.Session
. They may have been instantiated by the application and not yet persisted, or they may have been instantiated by a closedorg.hibernate.Session
. Chapter 11, Working with objects discusses transient, persistent and detached object states.- Transaction (
org.hibernate.Transaction
) (Optional) A single-threaded, short-lived object used by the application to specify atomic units of work. It abstracts the application from the underlying JDBC, JTA or CORBA transaction. A
org.hibernate.Session
might span severalorg.hibernate.Transaction
s in some cases. However, transaction demarcation, either using the underlying API ororg.hibernate.Transaction
, is never optional.- ConnectionProvider (
org.hibernate.connection.ConnectionProvider
) (Optional) A factory for, and pool of, JDBC connections. It abstracts the application from underlying
javax.sql.DataSource
orjava.sql.DriverManager
. It is not exposed to application, but it can be extended and/or implemented by the developer.- TransactionFactory (
org.hibernate.TransactionFactory
) (Optional) A factory for
org.hibernate.Transaction
instances. It is not exposed to the application, but it can be extended and/or implemented by the developer.- Extension Interfaces
Hibernate offers a range of optional extension interfaces you can implement to customize the behavior of your persistence layer. See the API documentation for details.
2.2. Contextual sessions
Most applications using Hibernate need some form of "contextual" session, where a given session is in effect throughout the scope of a given context. However, across applications the definition of what constitutes a context is typically different; different contexts define different scopes to the notion of current. Applications using Hibernate prior to version 3.0 tended to utilize either home-grown ThreadLocal
-based contextual sessions, helper classes such as HibernateUtil
, or utilized third-party frameworks, such as Spring or Pico, which provided proxy/interception-based contextual sessions.
Starting with version 3.0.1, Hibernate added the SessionFactory.getCurrentSession()
method. Initially, this assumed usage of JTA
transactions, where the JTA
transaction defined both the scope and context of a current session. Given the maturity of the numerous stand-alone JTA TransactionManager
implementations, most, if not all, applications should be using JTA
transaction management, whether or not they are deployed into a J2EE
container. Based on that, the JTA
-based contextual sessions are all you need to use.
However, as of version 3.1, the processing behind SessionFactory.getCurrentSession()
is now pluggable. To that end, a new extension interface, org.hibernate.context.spi.CurrentSessionContext
, and a new configuration parameter, hibernate.current_session_context_class
, have been added to allow pluggability of the scope and context of defining current sessions.
See the Javadocs for the org.hibernate.context.spi.CurrentSessionContext
interface for a detailed discussion of its contract. It defines a single method, currentSession()
, by which the implementation is responsible for tracking the current contextual session. Out-of-the-box, Hibernate comes with three implementations of this interface:
org.hibernate.context.internal.JTASessionContext
: current sessions are tracked and scoped by aJTA
transaction. The processing here is exactly the same as in the older JTA-only approach. See the Javadocs for details.org.hibernate.context.internal.ThreadLocalSessionContext
:current sessions are tracked by thread of execution. See the Javadocs for details.org.hibernate.context.internal.ManagedSessionContext
: current sessions are tracked by thread of execution. However, you are responsible to bind and unbind aSession
instance with static methods on this class: it does not open, flush, or close aSession
.
The first two implementations provide a "one session - one database transaction" programming model. This is also known and used as session-per-request. The beginning and end of a Hibernate session is defined by the duration of a database transaction. If you use programmatic transaction demarcation in plain JSE without JTA, you are advised to use the Hibernate Transaction
API to hide the underlying transaction system from your code. If you use JTA, you can utilize the JTA interfaces to demarcate transactions. If you execute in an EJB container that supports CMT, transaction boundaries are defined declaratively and you do not need any transaction or session demarcation operations in your code. Refer to Chapter 13, Transactions and Concurrency for more information and code examples.
The hibernate.current_session_context_class
configuration parameter defines whichorg.hibernate.context.spi.CurrentSessionContext
implementation should be used. For backwards compatibility, if this configuration parameter is not set but a org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform
is configured, Hibernate will use the org.hibernate.context.internal.JTASessionContext
. Typically, the value of this parameter would just name the implementation class to use. For the three out-of-the-box implementations, however, there are three corresponding short names: "jta", "thread", and "managed".
- Hibernate体系结构
- Hibernate 体系结构
- Hibernate体系结构
- hibernate体系结构
- Hibernate体系结构(简介)
- Hibernate的体系结构
- Hibernate的体系结构
- Hibernate的体系结构
- Hibernate的体系结构
- Hibernate的体系结构
- Hibernate基本用法:体系结构
- Hibernate 体系结构与工作原理
- Atitit.hibernate体系结构大总结
- Hibernate(一)hibernate基本概念和体系结构
- Hibernate学习之---Hibernate的体系结构
- Hibernate的工作原理和体系结构
- Hibernate中体系结构的概况(Overview)
- Hibernate 体系结构与工作原理 【附图】
- ios翻转
- linux内核
- iOS标准时间与时间戳相互转换
- PAT-B 1053. 住房空置率
- Ubuntu下deb包的安装方法
- Hibernate体系结构
- HDU 5067 动态规划---旅行商(tsp)问题
- 机器学习实战学习笔记2——决策树算法
- 8086汇编语言 数据段出现字符串
- Ionic幻灯片
- Codeforces Round #364 div.2 C. They Are Everywhere 【尺追法】
- 常见问题
- Git命令:将当前目录文件移动到上级目录
- Java 8 数据流教程