Many JPA storage units for dev / qa / stage / production

I was looking for answers for this, but I could not find such questions for this very suitable community!

  • I have a standalone Java application that is deployed in many environments: dev, qa, stage, production. Thus, each environment has its own datasource / db, and there are property files that manage different properties depending on the environment from which the application is running. So in my persistence.xml, I defined a persistence block for dev. In the same file, I would also like to define storage units for other environments. At the same time, Eclipse (Indigo - the latter) complains as follows: "Several units of life expectancy are determined - only the first persistence block will be recognized." I assume that what I did is legal and that is an Eclipse question .. can anyone confirm? Also, is this what best practices will dictate my current settings?
  • I was on the assumption that any bean entity marked with @Entity annotation will be automatically retrieved without explicitly defining it in the persistence.xml file as follows: <class>com.mycompany.model.MyEntityBean</class> . If I omit the explicit inclusion of an entity class in a file, the bean object - although annotated - generates an error: "The class" com.mycompany.model.MyEntityBean "is displayed, but not included in any persistence unit." What do I understand, am I mistaken?
  • My last question is about db credentials: is it better to put my db credentials in persistence.xml in plain text? Are there any safer alternatives to this?

Thank you community!

ps - I am not using EclipseLink as a JPA provider so that it matters

Here is an example of my persistence.xml

 <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="Development"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>com.mycompany.model.MyEntityBean</class> <properties> <property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:db2://xxxxxxx" /> <property name="javax.persistence.jdbc.password" value="xxxxxx" /> <property name="javax.persistence.jdbc.user" value="xxxxxxxx" /> </properties> </persistence-unit> <persistence-unit name="QA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>com.mycompany.model.MyEntityBean</class> <properties> <property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:db2://xxxxxxx" /> <property name="javax.persistence.jdbc.password" value="xxxxxx" /> <property name="javax.persistence.jdbc.user" value="xxxxxxxx" /> </properties> </persistence-unit> </persistence> 
+7
source share
2 answers
  • β€œMany units of duration β€” only the first unit to be recognized” is an Eclipse (Dali) question. You can find from error 231527 .

  • Since you have a standalone Java SE application, the objects must be listed in the persistence.xml file. This is stated in the specification as follows:

  To insure the portability of a Java SE application, it is necessary to explicitly list the managed persistence classes that are included in the persistence unit using the class element of the persistence.xml file. 
  • If you do not want to have passwords as plain text, some information about the alternative can be found in the EclipseLink documentation .
+4
source

You can disable the erroneous warning by doing the following. These instructions are for the Eclipse Luna.

 Project Properties => JPA => Errors/Warnings 

Check Enable project specific settings

Expand Project Group

Change Multiple persistence units defined: to Ignore .

+1
source

All Articles