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.
我注意到调用createEntityManagerFactory(null)将使用配置文件中的默认持久性单元(PU)。有时类路径在部署时变得非常混乱,我真的很想看到给定EntityManagerFactory的当前PU的名称。有没有办法做到这一点?另外,我希望能够看到EntityManagerFactory的整个属性映射。
2 个解决方案
#1
0
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;
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.
请注意,em是从EntityManagerFactory创建的打开的EntityManager。还有,像评论一样?没错,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.
我会为更稳定的JPA提供商提出任何好的建议。 Hibernate变得太不稳定了。这绝对不是我必须解决的第一个此类问题。
#2
0
The problem with Jan's answer is this:
Jan的答案的问题是:
Session s=(Session)em.getDelegate();
Following J2EE api, EntityManager.getDelegate() method : Return the underlying provider object for the EntityManager, if available. The result of this method is implementation specific. The unwrap method is to be preferred for new applications.
遵循J2EE api,EntityManager.getDelegate()方法:返回EntityManager的基础提供程序对象(如果可用)。此方法的结果是特定于实现的。解包方法是新应用程序的首选方法。
Due to the fact that the returned object is implementation specific it may vary so there's no reliable object or attribute from which take persistence unit's name.
由于返回的对象是特定于实现的事实,它可能会有所不同,因此没有可靠的对象或属性,从中获取持久性单元的名称。
I propose another easy way to get. It's as easy as add to the persistence.xml's persistence-units one custom property with it's name as the following example:
我提出了另一个简单的方法。它就像添加到persistence.xml的持久性一样容易 - 使用它的名称单位一个自定义属性,如下例所示:
<persistence-unit name="PERSISTENCE_UNIT" transaction-type="JTA">
<jta-data-source>DATASOURCE</jta-data-source>
<class>com.package.EntityClass</class>
<properties>
<property name="PERSISTENCE_UNIT_NAME" value="THE NAME OF MY PERSISTENCE UNIT"/>
</properties>
</persistence-unit>
</persistence>
and in your java code:
在你的java代码中:
Map<String, Object> propertiesMap = entityManagerInstance.getProperties();
String persistenceUnitName = (String)propertiesMap.get("PERSISTENCE_UNIT_NAME");
EntityManager's getProperties() method is a reliable method and not vendor specific so if you set you custom property with the name of your persistence-unit at your persistence.xml you will be able to recover it later in your java code easily.
EntityManager的getProperties()方法是一种可靠的方法,而不是供应商特定的,因此如果您在persistence.xml中使用持久性单元的名称设置自定义属性,则可以在以后的Java代码中轻松恢复它。
#1
0
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;
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.
请注意,em是从EntityManagerFactory创建的打开的EntityManager。还有,像评论一样?没错,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.
我会为更稳定的JPA提供商提出任何好的建议。 Hibernate变得太不稳定了。这绝对不是我必须解决的第一个此类问题。
#2
0
The problem with Jan's answer is this:
Jan的答案的问题是:
Session s=(Session)em.getDelegate();
Following J2EE api, EntityManager.getDelegate() method : Return the underlying provider object for the EntityManager, if available. The result of this method is implementation specific. The unwrap method is to be preferred for new applications.
遵循J2EE api,EntityManager.getDelegate()方法:返回EntityManager的基础提供程序对象(如果可用)。此方法的结果是特定于实现的。解包方法是新应用程序的首选方法。
Due to the fact that the returned object is implementation specific it may vary so there's no reliable object or attribute from which take persistence unit's name.
由于返回的对象是特定于实现的事实,它可能会有所不同,因此没有可靠的对象或属性,从中获取持久性单元的名称。
I propose another easy way to get. It's as easy as add to the persistence.xml's persistence-units one custom property with it's name as the following example:
我提出了另一个简单的方法。它就像添加到persistence.xml的持久性一样容易 - 使用它的名称单位一个自定义属性,如下例所示:
<persistence-unit name="PERSISTENCE_UNIT" transaction-type="JTA">
<jta-data-source>DATASOURCE</jta-data-source>
<class>com.package.EntityClass</class>
<properties>
<property name="PERSISTENCE_UNIT_NAME" value="THE NAME OF MY PERSISTENCE UNIT"/>
</properties>
</persistence-unit>
</persistence>
and in your java code:
在你的java代码中:
Map<String, Object> propertiesMap = entityManagerInstance.getProperties();
String persistenceUnitName = (String)propertiesMap.get("PERSISTENCE_UNIT_NAME");
EntityManager's getProperties() method is a reliable method and not vendor specific so if you set you custom property with the name of your persistence-unit at your persistence.xml you will be able to recover it later in your java code easily.
EntityManager的getProperties()方法是一种可靠的方法,而不是供应商特定的,因此如果您在persistence.xml中使用持久性单元的名称设置自定义属性,则可以在以后的Java代码中轻松恢复它。