我有一个使用多个持久性单元的 JavaEE 项目。有没有办法指定特定 JPA 实体属于哪个持久性单元?一些实体在一个数据源中,而其他实体在我的第二个数据源中。有没有办法使用注释来区分两者?
最佳答案
指定哪个持久单元是Entity
属于,使用 persistence.xml
文件:
<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>
注意 <exclude-unlisted-classes />
的使用.
关于database - JPA 实体 - 指定持久性单元?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16204565/