Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA multiple persistence units for dev/qa/stage/production

I've looked for answers for this but haven't been able to find any so asking these questions to this very apt community!

  1. I have a standalone java application that gets deployed in many environments: dev, qa, stage, production. As such each environment has its own datasource/db and there are property files that govern different properties depending upon which environment the application is running from. As such, in my persistence.xml I've defined a persistence unit for dev. In the same file, I'd like to define the persistence units for the other environments as well. When doing so, Eclipse (Indigo - latest) complains as follows: "Multiple persistence units defined - only the first persistence unit will be recognized". I'm assuming that what I've done is legal and this is an Eclipse issue.. can anyone confirm? Also, is this what best practices would dictate given my current setup?
  2. I was under the assumption that any entity bean marked with a @Entity annotation would automatically get picked up without having to explicitly define it in the persistence.xml file like so: <class>com.mycompany.model.MyEntityBean</class>. If I omit the explicit inclusion of the entity class in the file, the entity bean - although annotated - throws an error: "Class "com.mycompany.model.MyEntityBean" is mapped, but is not included in any persistence unit" What I have I assumed wrong?
  3. My final question is regarding db credentials: is it best practice to put my db credentials in the persistence.xml file in plain text? Are there any safer alternatives to this?

Thank you community!

p.s - I'm using EclipseLink as the JPA vendor not that it should matter??

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>    
like image 595
ustad Avatar asked Jun 08 '12 15:06

ustad


2 Answers

  1. "Multiple persistence units defined - only the first persistence unit will be recognized" is Eclipse (Dali) issue. More about subject you can find from bug 231527.

  2. Because you have standalone Java SE application, entities should be listed in persistence.xml. In specification this is spelled out 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.
  1. If you do not want to have passwords as plain text, some information about alternative can be found from EclipseLink documentation.
like image 57
Mikko Maunu Avatar answered Sep 27 '22 18:09

Mikko Maunu


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

Project Properties => JPA => Errors/Warnings

Check Enable project specific settings

Expand Project group

Change Multiple persistence units defined: to Ignore.

like image 36
AllanT Avatar answered Sep 27 '22 18:09

AllanT