JPA multiple persistence units for dev/qa/stage/production
Asked Answered
B

2

7

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>    
Breskin answered 8/6, 2012 at 15:45 Comment(2)
Can you add your persistence.xml content, it would help everyone better to understand.Falsehood
Hi Nayan, I've added an snipped of my persistence.xml thanks!Breskin
G
4
  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.
Ganoid answered 8/6, 2012 at 17:31 Comment(2)
Thanks for the info Mikko! I would have up-voted but I don't qualify as yet :)Breskin
Thanks Mikko, I've accepted the answer. It appears multiple persistence units are indeed being honored and it is the eclipse built-in JPA plugin that is complainingBreskin
S
1

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.

Subchaser answered 29/4, 2015 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.