我正在使用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创建额外的表,反之亦然。有什么建议在这里出错吗?
答案 0 :(得分:2)
在两个持久性单元中使用<exclude-unlisted-classes>true</exclude-unlisted-classes>
。根据{{3}}实体,未在特定持久性单元中列出的实体将不受此单元管理!
更新:根据this document
中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-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/>
而不是上面指定的配置。