persistence.xml 中的多个持久性单元相互创建表

Posted

技术标签:

【中文标题】persistence.xml 中的多个持久性单元相互创建表【英文标题】:Multiple persistance unit in persistence.xml creating tables in one another 【发布时间】:2014-02-18 12:33:25 【问题描述】:

我正在使用 JPA(休眠)并具有以下 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 创建额外的表,反之亦然。任何建议这里出了什么问题?

【问题讨论】:

【参考方案1】:

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

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

由持久性单元管理的托管持久性类的集合是通过使用一个或 更多以下内容:[81]

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

参考下面是exclude-unlisted-classesxsd

<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>

&lt;exclude-unlisted-classes&gt; 的默认值已更改为true,如果您使用JPA 2 进行实施,则应仅使用&lt;exclude-unlisted-classes/&gt; 而不是上面指定的配置。

【讨论】:

它就像一个魅力。虽然我使用上面建议的方式遇到了异常,但我使用了这样的标签“ @Ady.Q 是的,JPA2 规范似乎发生了变化

以上是关于persistence.xml 中的多个持久性单元相互创建表的主要内容,如果未能解决你的问题,请参考以下文章

springDataJpa的配置文件persistence.xml

JPA 使用替代“persistence.xml”

创建没有 persistence.xml 配置文件的 JPA EntityManager

我的 persistence.xml 文件中的 SAX 解析器错误

使用JNDI明显慢于persistence.xml中的显式连接(Jetty 9 / Hibernate)

Flowable入门系列文章71 - JPA用法