Hibernate.orgCommunity Documentation

第2章 アーキテクチャ

2.1. 概観
2.2. インスタンスの状態
2.3. JMX との統合
2.4. JCA サポート
2.5. コンテキスト上のセッション

Hibernate アーキテクチャの(非常に)高いレベルからのビュー:

We do not have the scope in this document to provide a more detailed view of all the runtime architectures available; Hibernate is flexible and supports several different approaches. We will, however, show the two extremes: "minimal" architecture and "comprehensive" architecture.

この図は Hibernate が、アプリケーションに対して永続化サービス (と永続オブジェクト)を提供するために、データベースと設定データを使うことを示しています。

The "minimal" architecture has the application provide its own JDBC connections and manage its own transactions. This approach uses a minimal subset of Hibernate's APIs:

The "comprehensive" architecture abstracts the application away from the underlying JDBC/JTA APIs and allows Hibernate to manage the details.

以下は、図に含まれるオブジェクトの定義です:

SessionFactory (org.hibernate.SessionFactory)

A threadsafe, immutable cache of compiled mappings for a single database. A factory for Session and a client of ConnectionProvider, SessionFactory can hold an optional (second-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. It wraps a JDBC connection and is a factory for Transaction. Session holds a mandatory first-level cache of persistent objects that are used when navigating the object graph or looking up objects by identifier.

Persistent objects と Collections

永続化状態とビジネス機能を持つ、短命でシングルスレッドのオブジェクト。これは通常の JavaBeans/POJO のこともありますが、特徴的なことは、その時点での(ただ1つの) Session と関連していることです。 Session がクローズされるとすぐに、それらは切り離されて他のアプリケーション層から自由に使うことができます(例えばデータトランスファオブジェクトとして、プレゼンテーション層から、またはプレゼンテーション層へ直接使用できます)。

Transient と detached な objects と Collections

現時点では Session と関連していない、永続クラスのインスタンス。すでにアプリケーション側でインスタンス化されていて、まだ永続化されていないか、クローズされた Session でインスタンス化されたかのどちらかです。

Transaction (org.hibernate.Transaction)

(オプション) 原子性を持つ作業単位 (Unit of Work) を指定するために、アプリケーションが使用する、シングルスレッドで短命なオブジェクト。下に位置する JDBC 、 JTA 、 CORBA トランザクションからアプリケーションを抽象化します。 Session は、時にはいくつかの Transaction をまたがるかもしれません。しかし、下の層の API を使うにせよ、 Transaction を使うにせよ、トランザクション境界を設定することは、決してオプションではありません。

ConnectionProvider (org.hibernate.connection.ConnectionProvider)

(オプション) JDBC コネクション(とそのプール)のファクトリ。下の層に位置する DatasourceDriverManager からアプリケーションを抽象化します。アプリケーションには公開されませんが、開発者が継承または実装することは可能です。

TransactionFactory (org.hibernate.TransactionFactory)

(オプション) Transaction インスタンスのファクトリ。アプリケーションには公開されませんが、開発者が継承または実装することは可能です。

Extension Interfaces

Hibernate は、永続層の振る舞いをカスタマイズするために、多くのオプション拡張インタフェースを用意しています。詳細は API ドキュメントを参照してください。

「軽い」アーキテクチャでは、アプリケーションは直接 JTA や JDBC と対話するために、 TransactionTransactionFactoryConnectionProvider の API をバイパスします。

永続クラスのインスタンスは、次の3つの異なる状態のどれかになります。それは、 永続コンテキスト によって決まります。 Hibernate の Session オブジェクトが、永続コンテキストになります:

JMX は Java コンポーネント管理の J2EE 標準です。 JMX 標準サービスを通して、 Hibernate は管理されます。ディストリビューションの中に org.hibernate.jmx.HibernateService という MBean 実装を用意しています。

JBoss アプリケーションサーバー上に Hibernate を JMX サービスとしてデプロイする方法の例としては、 JBoss ユーザーガイドを参照してください。 JBoss アプリケーションサーバーにおいて、 JMX を使ってデプロイすると、次のメリットが得られます:

これらのオプションについての詳細な情報は、 JBoss アプリケーションサーバーユーザーガイドを参考にしてください。

Another feature available as a JMX service is runtime Hibernate statistics. See 「Hibernate 統計」 for more information.

Hibernate は JCA コネクタとしても設定できます。詳細については、 Web サイトを見てください。 Hibernate JCA サポートは、今のところ実験段階として考えられていることに注意してください。

Hibernate を使ったアプリケーションは、ほとんど、なんらかの形で「コンテキスト上の」セッションが必要になります。「コンテキスト上のセッション」は、特定のコンテキストのスコープのなかで有効なセッションのことです。しかし、通常アプリケーションごとにコンテキストを構成するものの定義は異なります。しかも、異なる複数のコンテキストは、現時点に対して異なるスコープを定義します。バージョン3.0より前の Hibernate では、自作の ThreadLocal ベースの「コンテキスト上のセッション」を利用するか、 HibernateUtil のようなヘルパークラスを利用するか、 proxy/interception ベースの「コンテキスト上のセッション」を提供する (Spring や Pico のような)サードパーティのフレームワークを利用するかのいずれかでした。

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.

しかし、バージョン 3.1 からは、 SessionFactory.getCurrentSession() の後の処理が、プラガブルになりました。これを受けて、現在のセッションを定義するスコープとコンテキストのプラガビリティを可能にするために、新しい拡張インタフェース ( org.hibernate.context.CurrentSessionContext ) と新しい構成パラメータ ( hibernate.current_session_context_class ) が追加されました。

See the Javadocs for the org.hibernate.context.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:

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 12章Transactions and Concurrency for more information and code examples.

hibernate.current_session_context_class 設定パラメータは、 org.hibernate.context.CurrentSessionContext のどの実装を使うかを指定します。下位互換性のため、このパラメータが設定されず org.hibernate.transaction.TransactionManagerLookup が設定されていた場合、 Hibernate は org.hibernate.context.JTASessionContext を使うことに注意してください。通常このパラメータの値には、3つの実装の中から使用する実装クラスの名前を直接指定します。しかし、"jta"、 "thread"、 "managed"というそれぞれの省略名も用意されています。