java - persistence.xml 中的多个持久性单元在彼此中创建表

标签 java database hibernate jpa orm

我正在使用 JPA( hibernate )并具有以下 persistence.xml

<persistence version="1.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_1_0.xsd">
    <persistence-unit name="DB1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.dto1.AccessRight</class>
        <class>com.dto1.Component</class>
        <class>com.dto1.UserRight</class>      
        <properties>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
    <persistence-unit name="DB2" transaction-type="RESOURCE_LOCAL">
        <class>com.dto2.Auditlog</class>
        <properties>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

在代码中,我使用以下方法通过以下方式获取 EntityManager 工厂:

private static final EntityManagerFactory emf_db1 = Persistence.createEntityManagerFactory(DB1_PU_NAME, getConnectionProps(DB1_PU_NAME));
    private static final EntityManagerFactory emf_db2 = Persistence.createEntityManagerFactory(DB2_PU_NAME, getConnectionProps(DB2_PU_NAME));

    private static Map<String, String> getConnectionProps(String pu) {
        Map<String, String> dbConfProps = null;
        dbConfProps = new HashMap<String, String>();
        // Configure the Database properties
        ConnectionEntity conn_en = ConnectionEntity.getConnectionEntity();
        dbConfProps.put("hibernate.dialect", conn_en.getDbdialect());
        if (pu.equals(DB2_PU_NAME)) {
            dbConfProps.put("hibernate.connection.url", conn_en.getDB2_dburl());
        } else {
            dbConfProps.put("hibernate.connection.url", conn_en.getDB1_dburl());
        }
        dbConfProps.put("hibernate.connection.driver_class", conn_en.getDriver());
        dbConfProps.put("hibernate.connection.username", conn_en.getUsername());
        dbConfProps.put("hibernate.connection.password", conn_en.getPassword());

        return dbConfProps;
    }

    public static javax.persistence.EntityManager getInstance(String persistanceUnit) {

        logger.log("getInstance entered");
        if (persistanceUnit.equalsIgnoreCase(DB1_PU_NAME)) {
            return emf_idm.createEntityManager();
        }
        return emf_logs.createEntityManager();
    }

其中 conn_en 在属性文件中具有 dbConfiguration 并从中读取。发生的事情是,每当我的应用程序执行某些任务时,两个数据库都会在运行时创建彼此的表。在执行期间,我必须在两个数据库的表中进行输入。 DB1 从 DB2 创建额外的表,反之亦然。有什么建议这里出了什么问题吗?

最佳答案

使用 <exclude-unlisted-classes>true</exclude-unlisted-classes>在您的两个持久性单元中。根据 this document未在特定持久性单元中列出的实体将不受该单元管理!

更新:根据 JPA 2 的新规范在 jsr317

The set of managed persistence classes that are managed by a persistence unit is defined by using one or more of the following:[81]

 • Annotated managed persistence classes contained in the root of the
   persistence unit (unless the exclude-unlisted-classes element is specified)

引用以下是exclude-unlisted-classes xsd

<xsd:element name="exclude-unlisted-classes" type="xsd:boolean" default="true" minOccurs="0">
<xsd:annotation>
    <xsd:documentation>
        When set to true then only listed classes and jars will 
        be scanned for persistent classes, otherwise the 
        enclosing jar or directory will also be scanned. 
        Not applicable to Java SE persistence units.
 </xsd:documentation>
</xsd:annotation>

默认值<exclude-unlisted-classes>已更改为 true如果您使用 JPA 2对于实现,应该使用 <exclude-unlisted-classes/>仅代替上面指定的配置。

关于java - persistence.xml 中的多个持久性单元在彼此中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21853994/

相关文章:

java - Spring实体管理器无法启动: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

java - 为什么 EntityManager 关闭了?

Java 将字符串传递给 SAX

java - 用测试数据填充 XML 文件

java - 从 Spark 读取 Teradata 时出错。它加载了表并显示了架构,但无法给出数据集结果

sql - 选择在 X 月和 Y 或 Z 月购买的客户

SQL 自然连接 2 个匹配列

java - 简单的java定时器不工作

mysql - 查询返回有关嵌套元素的数据

postgresql - 如何在 Spring Data JPA 实体和通过继承相关的表之间进行映射