是否可以共享 persistence.xml 中的配置?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很奇怪。根据 HibernateProvider#createEntityManagerFactory 的来源,该方法返回一个新实例:
并且我在这个虚拟测试中绝对没有得到相同的实例:
实际上,它只是有效(第二个实例正在使用被覆盖的属性)。
This is weird. According to the sources of
HibernateProvider#createEntityManagerFactory
, the method returns an new instance:And I definitely don't get the same instances in this dummy test:
Actually, it just works (and the second instance is using the overridden properties).