...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();
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.
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.
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.
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.
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.
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.
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.
You need to:
EntityManager
to EntityManagerImpl
(the Hibernate implementation)getFactory()
EntityManagerFactory
to HibernateEntityManagerFactory
getSessionFactory()
and cast it to SessionFactoryImpl
getConnectionProvider()
and cast it to the correct implementation. You can see the implementations here. I'll assume that it's a DatasourceConnectionProvider
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.
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With