33
votes

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!

2
Are they runtime errors or Eclipse warnings? - axtavt
@axtavt: In persistence.xml is warning, but in the entity class that I create and put in the second <persistence-unit> with <class> tag, is error. The error is in class, not in xml file - artaxerxe

2 Answers

43
votes

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.

32
votes

While this is JPA valid, you may also turn off the resulting (annoying) error notifications on a per-project basis by going into

Project Properties => JPA => Errors/Warnings

Activate Enable project specific settings and under Type, change the notification level for Class is mapped but is not in a persistence unit from Error to anything else you're comfortable with.

Update for Eclipse Luna:

The location and name of the setting has changed in Luna. The new location is:

Project Properties => JPA => Errors/Warnings

Check Enable project specific settings

Expand Project group

Change Multiple persistence units defined: to Ignore.

enter image description here