我使用JPA而没有Spring连接到具有不同结构的多个postgreSql数据库。我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0">
<persistence-unit name="central" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- <provider>org.hibernate.ejb.HibernatePersistence</provider> -->
<class>model.central.Invoice</class>
<!-- <exclude-unlisted-classes>false</exclude-unlisted-classes>-->
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://xxx.xx.xxx.xx:5432/central"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.username" value="admin"/>
<property name="hibernate.connection.password" value="xxxx"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.flushMode" value="FLUSH_AUTO"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="validate"/> -->
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</properties>
</persistence-unit>
<persistence-unit name="S1A" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- <provider>org.hibernate.ejb.HibernatePersistence</provider> -->
<class>model.customer</class>
<!-- <exclude-unlisted-classes>false</exclude-unlisted-classes>-->
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://xxx.xx.xx.xx:5432/S1A"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.username" value="admin"/>
<property name="hibernate.connection.password" value="xxxx"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.flushMode" value="FLUSH_AUTO"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="validate"/> -->
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</properties>
</persistence-unit>
出于某种原因,当我坚持我的客户模型对象时,我也看到在两个数据库上都创建了Invoice表。当我坚持使用这些对象时,我期待在我的“中心”数据库和我的“S1A”数据库中的Customer表中看到Invoice表。但是,当我持久化一个对象时,两个数据库都以Customer和Invoice表结束。
这是我的DAO课程:
public class Dao {
private String dbname;
private static Map<String, EntityManager> emMap;
private static EntityManagerFactory entityManagerFactoryCentral;
private static EntityManager entityManagerCentral;
private static EntityManagerFactory entityManagerFactoryS1A;
private static EntityManager entityManagerS1A;
static {
emMap = new HashMap<String, EntityManager>();
entityManagerFactoryCentral = Persistence.createEntityManagerFactory("central");
entityManagerCentral = entityManagerFactoryCentral.createEntityManager();
emMap.put("central", entityManagerCentral);
entityManagerFactoryS1A = Persistence.createEntityManagerFactory("S1A");
entityManagerS1A = entityManagerFactoryS1A.createEntityManager();
emMap.put("S1A", entityManagerS1A);
}
public Dao(String dbname) {
this.dbname = dbname;
}
public void persist( Object... objects ){
EntityManager entityManager = emMap.get(dbname);
try {
entityManager.getTransaction().begin();
for ( Object object : objects ){
entityManager.persist(object);
}
entityManager.getTransaction().commit();
} catch( Exception e ){
e.printStackTrace();
try {
entityManager.getTransaction().rollback();
} catch (Exception e2){
e2.printStackTrace();
}
}
}
所以,这里的问题是如何在没有Spring的情况下使用一个persistence.xml文件,并使用JPA连接到model.central和model包下不同类列表的多个数据库。
答案 0 :(得分:0)
对不起这篇文章。我正在编辑驻留在war / META-INF下的错误的persistent.xml文件,而不是类路径src / META-INF。另外,我必须将true添加到持久性单元。