是否可以共享 persistence.xml 中的配置?

发布于 2024-09-17 19:24:24 字数 635 浏览 3 评论 0原文

我在 persistence.xml 中配置了一个持久单元,但我有两个数据库。这些数据库在架构方面是相同的。我想做的是:

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

属性包含不同的连接设置(用户、密码、jdbc url,...)。
我实际上尝试过这个,似乎 hibernate (我的 jpa 提供程序)在第二次调用中返回相同的实例,而不处理属性。

我需要将配置复制到第二个单元吗?


我把它归结为与我之前想象的不同的东西。上述调用返回的 EntityManager(和工厂)按预期工作,但 getDelegate() 似乎是问题所在。我需要获取底层会话来支持我的应用程序中直接依赖于 hibernate api 的遗留代码。我所做的是:

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

但不知何故,即使使用在第二个数据库上运行的实体管理器,我也会收到在主数据库上运行的会话。

I have one persistence unit configured in my persistence.xml but i have two databases. Those databases are identical, regarding the schema. What i am trying to do is:

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

The properties contain different connection settings (user, password, jdbc url, ...).
I tried this actually and it seems that hibernate (my jpa provider) returns the same instance in the second call, without taking care of the properties.

Do i need to copy the configuration to a second unit?


I nailed it down to something different than i thought before. The EntityManagers (and Factories) returned by the calls above work as expected, but getDelegate() seems to be the problem. I need to get the underlying session to support legacy code in my application which relies directly on the hibernate api. What i did is:

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

But somehow i receive a session operating on the primary database even when using an entitymanager which operates on the second.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-09-24 19:24:24

这很奇怪。根据 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
}

实际上,它只是有效(第二个实例正在使用被覆盖的属性)。

This is weird. According to the sources of HibernateProvider#createEntityManagerFactory, the method returns an new instance:

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

And I definitely don't get the same instances in this dummy test:

@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
}

Actually, it just works (and the second instance is using the overridden properties).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文