Sha*_*man 10 database jpa java-ee persistence-unit jpa-2.0
我有一个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>
Run Code Online (Sandbox Code Playgroud)
注意使用<exclude-unlisted-classes />.