Multiple persistence-unit tags in one persistence.xml

artaxerxe picture artaxerxe · Dec 6, 2011 · Viewed 23.8k times · Source

I'm working now with a JPA 2.0 project (in Eclipse Indigo). In my persistence.xml file I need to specify two <persistence-unit> tags, because I have two different database to work with. Doing this, I get the following warning:

Multiple persistence units defined - only the first persistence unit will be recognized

Even more, if I create my entities and put it in my second <persistence-unit>, I get an error saying that my entity isn't declared in a persistence-unit:

Class "my.package.MyClass" is mapped, but is not included in any persistence unit

So, the question is: is possible to declare (and use them as expected) many <persistence-unit> tags in persistence.xml file? If yes, what do I need to do for getting the accurate behavior?

Thanks!

Answer

Mikko Maunu picture Mikko Maunu · Dec 22, 2011

JPA does not limit number of persistence units defined in persistence.xml. This warning just tells about limitation of tool in Eclipse IDE. This is told for example here http://www.eclipse.org/webtools/dali/gettingstarted.php :

Currently Dali only supports one Persistence Unit and one Persistence XML file per project. Other configurations can exist in a JPA project, but the validation and defaults processing may not be correct when multiple persistence units are used.

I guess there is no known way to get this work in Eclipse IDE.