Reputation: 199
I would like to get the EntityManagerFactory instance from Persistence interface in a jee environment.. but the specification link : https://docs.oracle.com/javaee/7/api/javax/persistence/Persistence.html
states the following
The Persistence class is available in a Java EE container environment as well; however, support for the Java SE bootstrapping APIs is not required in container environments.
Does this mean that we should not be using Persistence.createEntityManagerFactory not to be used in a java EE environment ?
Note: I have tested at WAS 8.5.5 and JBOSS EAP 7 and able to successfully get the EntityManagerFactory and perform DB persistence. I wanted to know if as per specification the container may stop the support for this API ?
Upvotes: 0
Views: 280
Reputation: 1984
If I understand your comments correctly, your problem is that the data source is dynamic to some degree and therefore you want to use the bootstrapping API to create your EntityManagerFactory instead of configuring it "the EE way" through your persistence.xml
How dynamic is your datasource exactly? What EE container are you running? You might be able to configure the datasource in your container configuration (ie, on the server) and have your entitymanagerfactory either created by the container or automatically pick up the datasource through JNDI.
At a former employer we ran Wildfly and we configured the datasource in the wildfly configuration on every server, letting wildfly provide the datasource on a standard location for hibernate to pick up and construct the EMF.
See https://stackoverflow.com/a/41550908/691074 for an example of a similar setup.
This enabled us to run the same code on different servers connecting to different databases (ie, test, production, etc.)
The default wildfly configuration already configures an in-memory H2 database, you could modify that to point in the correct direction.
Here are the Wildfly 10 docs regarding datasource configuration and here is an explanation of the persistence.xml with examples for referencing a datasource managed by the container.
Would this work for your case?
Upvotes: 1