3.8.4. JMX デプロイメント

JNDI が SessionFactory を取得するには cfg.buildSessionFactory() 行を実行する必要があります。 これには、 static 初期化子ブロック内 (HibernateUtil 内のものなど) で行うか、 Hibernate を管理対象サービスとしてデプロイします。
Hibernate には、 JBoss AS など JMX 機能を持つアプリケーションサーバー上でのデプロイメント向けに、 org.hibernate.jmx.HibernateService が含まれています。 実際のデプロイメントや設定はベンダー固有となります 。JBoss 4.0.x 向けの jboss-service.xml の例は次のようになります。
<?xml version="1.0"?>
<server>

<mbean code="org.hibernate.jmx.HibernateService"
    name="jboss.jca:service=HibernateFactory,name=HibernateFactory">

    <!-- Required services -->
    <depends>jboss.jca:service=RARDeployer</depends>
    <depends>jboss.jca:service=LocalTxCM,name=HsqlDS</depends>

    <!-- Bind the Hibernate service to JNDI -->
    <attribute name="JndiName">java:/hibernate/SessionFactory</attribute>

    <!-- Datasource settings -->
    <attribute name="Datasource">java:HsqlDS</attribute>
    <attribute name="Dialect">org.hibernate.dialect.HSQLDialect</attribute>

    <!-- Transaction integration -->
    <attribute name="TransactionStrategy">
        org.hibernate.transaction.JTATransactionFactory</attribute>
    <attribute name="TransactionManagerLookupStrategy">
        org.hibernate.transaction.JBossTransactionManagerLookup</attribute>
    <attribute name="FlushBeforeCompletionEnabled">true</attribute>
    <attribute name="AutoCloseSessionEnabled">true</attribute>

    <!-- Fetching options -->
    <attribute name="MaximumFetchDepth">5</attribute>

    <!-- Second-level caching -->
    <attribute name="SecondLevelCacheEnabled">true</attribute>
    <attribute name="CacheProviderClass">org.hibernate.cache.EhCacheProvider</attribute>
    <attribute name="QueryCacheEnabled">true</attribute>

    <!-- Logging -->
    <attribute name="ShowSqlEnabled">true</attribute>

    <!-- Mapping files -->
    <attribute name="MapResources">auction/Item.hbm.xml,auction/Category.hbm.xml</attribute>

</mbean>

</server>
このファイルは META-INF ディレクトリにデプロイされ、 .sar 拡張子 (サービスアーカイブ) を持つ JAR ファイルにパッケージ化されます。 また、 Hibernate、 Hibernate が必要とするサードパーティーライブラリ、 コンパイルされた永続クラス、 マッピングファイルを同じアーカイブにパッケージ化する必要があります。 エンタープライズ Bean (通常はセッション Bean) は独自の JAR ファイルに保持することができますが、 この EJB JAR ファイルをメインサービスアーカイブに追加し、 単一の (ホット) デプロイ可能ユニットにすることができます。 JMX サービスや EJB デプロイメントに関する詳細は JBoss AS のドキュメントを参照してください。