查找当前EntityManagerFactory的当前持久性单元 [英] Finding the current persistence unit for the current EntityManagerFactory

查看:95
本文介绍了查找当前EntityManagerFactory的当前持久性单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到调用 createEntityManagerFactory(null)将使用配置文件中的默认持久性单元(PU)。有时类路径在部署时变得非常混乱,我真的很想看到给定EntityManagerFactory的当前PU的名称。有没有办法做到这一点?此外,我希望能够看到EntityManagerFactory的整个属性映射。

I noticed that calling createEntityManagerFactory(null) will use the default Persistence Unit (PU) in the configuration file. Sometimes the classpaths get really messed up at deployment and I'd really like to see the name of the current PU for a given EntityManagerFactory. Is there any way to do this? Also, I would love to go so far as to see the entire properties map for the EntityManagerFactory.

推荐答案

我找到了一条路如果提供者处于休眠状态,请执行以下操作:

I found a way to do it if the provider is 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 是一个已打开的EntityManager来自EntityManagerFactory。还有,像评论一样?没错,Hibernate团队再次充满惊喜。尽管用户提出反馈意见不要弃用可以帮助我的唯一方法,但他们正在全力以赴。注意此链接

Note that em is an opened EntityManager created from an EntityManagerFactory. Also, like the comments? That's right, the Hibernate team is full of surprises again. Even though users gave feedback begging not to deprecate the only method that can help me, they are going full steam ahead. Note this link.

我会为更稳定的JPA提供商提出任何好的建议。 Hibernate变得太不稳定了。这绝对不是我必须解决的第一个问题。

I'll upvote any good suggestions for a more stable JPA provider. Hibernate is just getting too unstable. This is definitely not the first issue of this kind that I had to workaround.

这篇关于查找当前EntityManagerFactory的当前持久性单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆