Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple persistence-unit tags in one persistence.xml

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!

like image 708
artaxerxe Avatar asked Dec 06 '11 10:12

artaxerxe


People also ask

Can we have multiple persistence xml?

The Java Persistence API allows you to define multiple persistence units, each of which can map to a separate database.

What is persistence unit in persistence xml?

A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application. This set of entity classes represents the data contained within a single data store.

Where should I put the persistence xml?

xml should be put in the EJB JAR's META-INF directory. If you package the persistence unit as a set of classes in a WAR file, persistence. xml should be located in the WAR file's WEB-INF/classes/META-INF directory.


1 Answers

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.

like image 106
Mikko Maunu Avatar answered Oct 09 '22 21:10

Mikko Maunu