Search for the current duration unit for the current EntityManagerFactory

I noticed that calling createEntityManagerFactory(null) will use the Persistence Unit (PU) parameter in the configuration file by default. Sometimes classpaths really get confused during 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? In addition, I would like to go so far as to see the entire property map for EntityManagerFactory.

+4
source share
2 answers

I found a way to do this if the provider is sleeping:

 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 open EntityManager created from EntityManagerFactory. Also, like the comments? Right, the Hibernate team is again full of surprises. Despite the fact that users asked us not to condemn the only method that can help me, they are in full swing. Note this link .

I will support any good suggestions for a more stable JPA provider. Sleep mode becomes too unstable. This is definitely not the first problem of this kind that I have had to deal with.

0
source

The problem with Jan's answer is this:

Session s = (session) em.getDelegate ();

Following the J2EE api, EntityManager.getDelegate (): Returns the main provider object for the EntityManager, if available. The result of this method is a concrete implementation. The deployment method should be preferred for new applications.

Due to the fact that the returned object is implementation-specific, it can change, so there is no reliable object or attribute from which the name of the storage unit follows.

I suggest another easy way to get. This is as simple as adding one custom property called name to persistence.xml persistence-units in the following example:

 <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:

 Map<String, Object> propertiesMap = entityManagerInstance.getProperties(); String persistenceUnitName = (String)propertiesMap.get("PERSISTENCE_UNIT_NAME"); 

The EntityManager getProperties () method is a reliable method and not vendor specific, so if you configure your own resource with the name of your persistence unit in your persistence.xml, you can easily restore it later in your java code.

0
source

All Articles