本文介绍了JPA实体 - 指定持久性单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用多个持久化单元的JavaEE项目。有什么方法来指定一个特定的JPA实体属于哪个持久化单元?一些实体位于一个数据源中,而其他实体位于我的第二个数据源中。是否有办法使用注释来区分这两个注释?
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?
推荐答案
要指定
属于,使用 persistence.xml
文件:
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>
请注意< exclude-unlisted-classes /> / code>。
Note the use of
<exclude-unlisted-classes />
.
这篇关于JPA实体 - 指定持久性单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!