我注意到调用 createEntityManagerFactory(null)
将使用配置文件中的默认持久性单元 (PU)。有时类路径在部署时真的很困惑,我真的很想看到给定 EntityManagerFactory 的当前 PU 的名称。有什么办法吗?此外,我很乐意看到 EntityManagerFactory 的整个属性映射。
最佳答案
如果提供程序处于 hibernate 状态,我找到了一种方法:
Session s=(Session)em.getDelegate();
//Get the URL.
String info=null;
try {
//The wonderful Hibernate team deprecated connection() before giving an alternative.
//Feel free to share the love at http://opensource.atlassian.com/projects/hibernate/browse/HHH-2603
//cf http://opensource.atlassian.com/projects/hibernate/browse/HHH-2603?focusedCommentId=29531&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_29531
info=s.connection().getMetaData().getURL();
} catch (Exception e) {
throw new RuntimeException(e);
}
//Finish
em.close();
return info;
请注意,em
是一个从 EntityManagerFactory 创建的打开的 EntityManager。另外,喜欢评论吗?没错,Hibernate团队又是惊喜连连。尽管用户给出的反馈恳求不要弃用唯一可以帮助我的方法,但他们正在全速前进。备注this link .
我会为更稳定的 JPA 提供程序提供任何好的建议。 Hibernate 变得太不稳定了。这绝对不是我必须解决的第一个此类问题。
关于java - 查找当前 EntityManagerFactory 的当前持久化单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2014925/