简体   繁体   中英

JPA Persistence Configuration for Multiple Database without Spring

I am using JPA without Spring connecting to multiple postgreSql database with different structure. My 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>

For some reason when I persist my customer model object I also see Invoice table gets created on both database. I was expecting to see Invoice table in my "cental" db and Customer table in my "S1A" database when I persist these object. However, as I persist one object both database end up with Customer and Invoice table.

This my DAO class:

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();
        }
     }
}

So, the question here is how to use one persistence.xml file without Spring and using JPA to connect to multiple database with different list of classes under model.central and model package.

Sorry for this post. I was editing the wrong persistent.xml file residing under war/META-INF instead of class path src/META-INF. In addition I had to add the true to persistence-unit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM