Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the datasource used by a persistence unit programmatically

...without actually reading and parsing the persistence.xml

I can retrieve the name of the persistence unit of an EntityManager using the properties of it's factory. I can retrieve the available datasources using the jboss-as-controller-client. But I have found no API that would give me the datasource of a particular EntityManager.

A String with a name would be enough.

Thank you

I am working with Hibernate 4.0.1.Final over JPA 2 on a JBoss 7.1.1.Final.

EDIT: and I would like to avoid straying from JPA to Hibernate APIs if possible.

EDIT : Augusto's solution worked, I have some notes on details: The casting of the EM didn't work because of a ClassCastException:(org.jboss.as.jpa.container.TransactionScopedEntityManager cannot be cast to org.hibernate.ejb.EntityManagerImpl), but it worked for the retrieved factory. So I omitted step 1.

I also could not find a way to retrieve the name of the datasource from the instance. So I had to content myself with the catalog name: connectionProvider.getConnection().getCatalog();

like image 945
kostja Avatar asked Sep 14 '12 11:09

kostja


People also ask

What is persistence xml Datasource?

The persistence. xml configuration file is used to configure a given JPA Persistence Unit. The Persistence Unit defines all the metadata required to bootstrap an EntityManagerFactory , like entity mappings, data source, and transaction settings, as well as JPA provider configuration properties.

Where can I find persistence xml?

If you package the persistence unit as a set of classes in a WAR file, persistence. xml should be located in the WAR file's WEB-INF/classes/META-INF directory.

Which API is created with the help of persistence unit?

The Java Persistence API implementation will create a table for the Player entity in your relational database. By default, the table name corresponds to the unqualified class name. In this case, a PLAYER table will represent Player entities.

Where do I put persistence unit in Java?

If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file’s WEB-INF/classes/META-INF directory. Note – In the Java Persistence API 1.0, JAR files could be located at the root of an EAR file as the root of the persistence unit.

What is persistence unit in JDBC?

Persistence Units. Persistence units are defined by the persistence.xml configuration file. This file defines a persistence unit named OrderManagement , which uses a JTA-aware data source: jdbc/MyOrderDB. The jar-file and class elements specify managed persistence classes: entity classes, embeddable classes, and mapped superclasses.

What is persistence unit in Salesforce?

A persistence unit defines a set of all entity classes that are managed by EntityManagerinstances in an application. This set of entity classes represents the data contained within a single data store.

How do I identify a persistent unit?

Each persistence unit must be identified with a name that is unique to the persistence unit’s scope. Persistent units can be packaged as part of a WAR or EJB JAR file or can be packaged as a JAR file that can then be included in an WAR or EAR file.


3 Answers

You need to:

  1. cast the EntityManager to EntityManagerImpl (the Hibernate implementation)
  2. call getFactory()
  3. cast the EntityManagerFactory to HibernateEntityManagerFactory
  4. call getSessionFactory() and cast it to SessionFactoryImpl
  5. call getConnectionProvider() and cast it to the correct implementation. You can see the implementations here. I'll assume that it's a DatasourceConnectionProvider
  6. call getDataSource() and you're done.

Unfortunately, you must use the Hibernate API, as there's no way to retrieve the DataSource using the JPA API.

like image 196
Augusto Avatar answered Oct 13 '22 12:10

Augusto


In a Spring environment you can use this:

import org.springframework.orm.jpa.EntityManagerFactoryInfo;
...

@PersistenceContext
EntityManager entityManager;

public DataSource getDataSourceFromHibernateEntityManager() {
   EntityManagerFactoryInfo info = (EntityManagerFactoryInfo) entityManager.getEntityManagerFactory();
   return info.getDataSource();
}
like image 24
Markus Umefjord Avatar answered Oct 13 '22 13:10

Markus Umefjord


If you just want the name of the datasource and that datasource name was supplied per JPA means, you should be able to get that information via:

entityManager.getEntityManagerFactory().getProperties().get( "javax.persistence.jtaDataSource" );

or

entityManager.getEntityManagerFactory().getProperties().get( "javax.persistence.nonJtaDataSource" );

depending on how you defined the datasource.

like image 9
Steve Ebersole Avatar answered Oct 13 '22 13:10

Steve Ebersole