質問

私はpersistence.xmlで構成された1つの永続性ユニットを持っているが、私は2つのデータベースを持っています。これらのデータベーススキーマについては、同一です。私は何をしようとしていることはあります:

Persistence.createEntityManagerFactory("unit", primaryProperties);
Persistence.createEntityManagerFactory("unit", secondaryProperties);

のプロパティは、異なる接続設定(ユーザー、パスワード、JDBC URLを、...)が含まれています。
私は実際にこれを試してみましたが、その休止状態(私のJPAプロバイダが)プロパティの世話をせずに、2回目の呼び出しで同じインスタンスを返すようです。

私は秒単位に設定をコピーする必要がありますか?

<時間>

私は私が前に考えていたよりも別の何かにそれを釘付け。予想通りEntityManagers(および工場は)仕事上の呼び出しによって返されたが、getDelegate()が問題のようです。私は、Hibernate APIに直接依存している私のアプリケーションでサポートレガシーコードへの根本的なセッションを取得する必要があります。私がやったことはあります:

final Session session = (Session) manager.getDelegate();

しかし、どういうわけか私は2番目で動作EntityManagerを使用した場合でも、プライマリ・データベース上で動作してセッションを受けています。

役に立ちましたか?

解決

これは奇妙です。 HibernateProvider#createEntityManagerFactoryの情報源によれば、この方法は、新しいインスタンスを戻します。

public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
    Ejb3Configuration cfg = new Ejb3Configuration();
    Ejb3Configuration configured = cfg.configure( persistenceUnitName, properties );
    return configured != null ? configured.buildEntityManagerFactory() : null;
}

そして、私は間違いなく、このダミーのテストで同じインスタンスを得ることはありません。

@Test
public void testCreateTwoDifferentEMF() {
    Map properties1 = new HashMap();
    EntityManagerFactory emf1 = Persistence.createEntityManagerFactory("MyPu", properties1);
    Map properties2 = new HashMap();
    properties2.put("javax.persistence.jdbc.user", "foo");
    EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("MyPu", properties2);
    assertFalse(emf1 == emf2); //passes
}

実際には、それだけで動作する(第二のインスタンスは、オーバーライドプロパティを使用している)。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top