0

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 ?

3
  • I am trying to get clarity on the specification. I would be interested to know the reason for downvote Commented Jun 21, 2019 at 19:10
  • First of all, "not required" only means that using this API may not be portable between JavaEE implementations. Normally you would @Inject your EntityManagerFactory, or better, just an EntityManager. Commented Jun 22, 2019 at 6:09
  • Hi Steve, the name of data source configured is dynamic and I can not hardcode it at persistence.xml. to be able to use a.mappong file or a custom datasoirce look up I need the provision to set them dynamically and I am able to achieve this using the Persistence.createEntotyManagerFactory API. Also it is not an option for me to maintain multiple Persistence.xml s considering the variation of dynamic Datasource names getting generated during the deployment environment. Commented Jun 23, 2019 at 2:52

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Buurman, Thanks for your response and detailed explanation.
The clarity which i aim to achive from this post is the Spec mentions the bootstrapping is not required to be supported at Java EE environment. Though this is currently supported at WAS 8.5 and JBOSS EAP 7 what other alternative do we have to inject the entitymanager if the support for Persistence API might be removed by the application servers.
Data source are configured Region specific and based on how many regions we have the application deployed that many data source names are defined. This number can grow and hence would like to keep it Dynamic. Also i referred to the other Stackover flow post which configured the DS using <jta-data-source>jdbc/myDS</jta-data-source>. The issue is i will not be able to set the DS name upfront during compilation time. This will be dynamic and Persistence API give the flexibility to achive the same.
If you configure the datasource in the wildfly configuration per server you can set whatever jndi name you want. It is not relevant for the database connection, only condition is that you use the same name everywhere (so you can compile your code using the at-runtime configured datasource to bulid the entitymanager. In your runtime wildfly configuration, you can configure the datasource using jdbc uri and database name whatever you want, only requirement is the jndi name should be the one you use in your persistence xml so your code can find (inject) the datasource.
As I understand your problem this should work as a solution. Whether you configure the datasource on the server and inject it, or configure something like environment variables (with jdbc uri and username etc.) and construct the datasource programmatically, as you seem to want, is the same thing if you look at what information you need to have available at what time, or not? As long as you know at runtime/on the specific server what jdbc uri and such to use to connect to the database, you can use either solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.