I have a JavaEE project that makes use of multiple persistence units. Is there any way to specify which persistence unit a particular JPA Entity belongs to? Some entities are in one data source, while others are in my second data source. Is there a way to differentiate between the two using annotations?
To specify which persistent unit an Entity
belongs to, use the persistence.xml
file:
<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="user" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/myApp</jta-data-source>
<class>com.company.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- properties -->
</properties>
</persistence-unit>
<persistence-unit name="data" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/myApp_data</jta-data-source>
<!--<mapping-file>META-INF/myApp_entities.xml</mapping-file> You can also use mapping files.-->
<class>com.company.Data</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- properties -->
</properties>
</persistence-unit>
</persistence>
Note the use of <exclude-unlisted-classes />
.
Also you can identify from which persistent unit an entity belongs by identifying the EntityManager that registered it.
A managed entity belongs to a persistence context, and a persistent context belongs to a persistence unit. So in this example:
@PersistenceContext(unitName="persistence-unit-1")
EntityManager em1;
@PersistenceContext(unitName="persistence-unit-2")
EntityManager em2;
em1.persist(entity1);
em2.persist(entity2);
entity1 belongs to persistence-unit-1 and entity2 belongs to persistence-unit-2. It's not so explicit like specifying the <class> tags in persistence.xml, but you can have the same entity classes in both persistent units and still differentiate to which unit each entity instance belongs.
persistence-unit
it belongs to? Look answer abouve... –
Chirrup The @PersistenceUnit
should be usable as well (I haven't tried it, yet, though)
e.g.
@PersistenceUnit(unitName="persistenceUnit2")
@Entity
class XPTO {
}
From the Javadoc (http://docs.oracle.com/javaee/6/api/javax/persistence/PersistenceUnit.html)
"Expresses a dependency on an EntityManagerFactory and its associated persistence unit."
unitName (Optional) The name of the persistence unit as defined in the persistence.xml file.
exclude-unlisted-classes=true
–
Chirrup PersistenceUnit
(one or more) they shall belong to without listing all entity classes in the persistence.xml... this is annoying for few hundreds entities... even for less. –
Chirrup exclude-unlisted-classes
is false. I use Wildfly 8.2.1 with the included hibernate. I think I must downvote this answer, since it is an nonworing advice. Or please bring a proof that it works. –
Chirrup @PersistenceUnit
annotation. I guess the purpose of the @PersistenceUnit
annotation is not to annotate entities. –
Punk © 2022 - 2024 — McMap. All rights reserved.