database - JPA 实体 - 指定持久性单元?

标签 database jakarta-ee jpa jpa-2.0 persistence-unit

我有一个使用多个持久性单元的 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/

相关文章:

jsp - 为什么iframe让服务器发出请求而不是客户端

java - 重复注释错误 - 但在哪里?

java - Intellij 如何使用 spring boot 正确配置 hql。现在我得到 Persistence QL Queries are error-checked

objective-c - 将 Sybase UltraLite Db 集成到我的代码中

database - 是否可以不将 Postgres 查询的结果写入磁盘?

MySQL 性能

java - 单元测试: @Inject not injecting service

java - WebLogic 集群 - 每周重新启动所有应用程序服务器实例?

hibernate - 调用entityManager.remove()时Hibernate会删除引用的实体吗?

ajax - WordPress 前端 AJAX 表单提交给出 500 内部服务器错误