About

ドキュメント

プロジェクト文書

Built by Maven

複数の永続ユニットの使い方

複数のJPA実装を使う場合や、複数のJDBC DataSourceを使う場合は、複数の永続ユニットを作成します。そして、それぞれの永続ユニットに対応するjpa.diconのコピーを用意し、jpa.diconからインクルードします。

  • persistence.xmlに複数の永続ユニットを定義します。
    <persistence>
    	<persistence-unit name="persistenceUnit" transaction-type="JTA">
    		<jta-data-source>jdbc/dataSource</jta-data-source>
    	...
    	</persistence-unit>
    	<persistence-unit name="fooPersistenceUnit" transaction-type="JTA">
    		<jta-data-source>jdbc/dataSource</jta-data-source>
    	...
    	</persistence-unit>
  • pu.diconを定義します。jpa.diconをコピーします。
    <components initializeOnCreate="true">
    	<include path="s2openjpa.dicon"/>
    
    	<component name="persistenceUnitProvider"
    		class="org.seasar.framework.jpa.impl.ContainerPersistenceUnitProvider">
    		<property name="unitName">"persistenceUnit"</property>
    		<property name="providerClassName">
    			"org.apache.openjpa.persistence.PersistenceProviderImpl"
    		</property>
    	</component>
    
    	<component name="entityManagerFactory" class="javax.persistence.EntityManagerFactory">
    		persistenceUnitProvider.entityManagerFactory
    	</component>
    
    	<component name="entityManager"
    		class="org.seasar.framework.jpa.impl.TxScopedEntityManagerProxy"/>
    </components>
  • foo-pu.diconを定義します。jpa.diconをコピーし、次のようにします。
    <components initializeOnCreate="true">
    	<include path="s2openjpa.dicon"/>
    
    	<component name="fooPersistenceUnitProvider"
    		class="org.seasar.framework.jpa.impl.ContainerPersistenceUnitProvider">
    		<property name="unitName">"fooPersistenceUnit"</property>
    		<property name="providerClassName">
    			"org.apache.openjpa.persistence.PersistenceProviderImpl"
    		</property>
    	</component>
    
    	<component name="fooEntityManagerFactory" class="javax.persistence.EntityManagerFactory">
    		fooPersistenceUnitProvider.entityManagerFactory
    	</component>
    
    	<component name="fooEntityManager"
    		class="org.seasar.framework.jpa.impl.TxScopedEntityManagerProxy"/>
    </components>
  • jpa.diconを書き換えpu.diconfoo-pu.diconをインクルードします。
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" 
        "http://www.seasar.org/dtd/components24.dtd">
    <components>
      <include path="pu.dicon"/>
      <include path="foo-pu.dicon"/>
    </components>
  • エンティティマネジャーを使用するDaoのクラスはプロパティでオブジェクトを受け取ります。プロパティ名には使用したいエンティティマネージャーのコンポーネント名を使用してください。
    public void setFooEntityManager(EntityManager fooEntityManager) { 
        this.fooEntityManager = fooEntityManager;
    }
    または、JPAのPersistenceContextアノテーションを使用してエンティティマネージャーを設定します。unitName要素には使用したい永続コンテキストの名称を指定してください。
    @PersistenceContext(unitName="fooPersistenceUnit")
    private EntityManager em