Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java persistence.xml中的多个持久性单元相互创建表_Java_Database_Hibernate_Jpa_Orm - Fatal编程技术网

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

Java persistence.xml中的多个持久性单元相互创建表,java,database,hibernate,jpa,orm,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/

我正在使用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>

org.hibernate.ejb.HibernatePersistence
com.dto1.AccessRight
com.dto1.com组件
com.dto1.UserRight
com.dto2.Auditlog
在代码中,我使用以下方法获取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();
    }
private static final EntityManagerFactory emf_db1=Persistence.createEntityManagerFactory(db1_PU_名称,getConnectionProps(db1_PU_名称));
私有静态最终EntityManagerFactory emf_db2=Persistence.createEntityManagerFactory(db2_PU_名称,getConnectionProps(db2_PU_名称));
私有静态映射getConnectionProps(字符串pu){
Map dbConfProps=null;
dbConfProps=newhashmap();
//配置数据库属性
ConnectionEntity conn_en=ConnectionEntity.getConnectionEntity();
dbConfProps.put(“hibernate.dialogue”,conn_en.getdbdialogue());
if(pu.equals(DB2_pu_NAME)){
dbConfProps.put(“hibernate.connection.url”,conn_en.getDB2_dburl());
}否则{
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());
返回道具;
}
公共静态javax.persistence.EntityManager getInstance(字符串persistenceUnit){
logger.log(“输入getInstance”);
if(persistanceUnit.equalsIgnoreCase(DB1_PU_NAME)){
返回emf_idm.createEntityManager();
}
返回emf_logs.createEntityManager();
}
其中conn_en在属性文件中具有dbConfiguration并从中读取。发生的事情是,每当我的应用程序执行某些任务时,两个数据库都会在运行时创建彼此的表。在执行过程中,我必须在两个数据库的表中创建条目。DB1从DB2创建额外的表,反之亦然。有什么问题吗?

在两个持久性单元中使用
true
。根据未在特定持久性单元中列出的实体,该单元将不受管理

更新:根据JPA 2的新规范

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

 • Annotated managed persistence classes contained in the root of the
   persistence unit (unless the exclude-unlisted-classes element is specified)
关于这一点,下面是
排除未列出的类
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>

当设置为true时,将只显示列出的类和jar
扫描持久类,否则
封闭的jar或目录也将被扫描。
不适用于Java SE持久化单元。


的默认值已更改为
true
如果您使用
JPA 2
进行实施,则应仅使用
而不是上面指定的配置。

它工作起来很有魅力。虽然我使用上面建议的方法遇到了异常,但我使用了如下标记“@Ady.Q yah”,JPA2规范中似乎有变化