Hibernate using multiple databases

后端 未结 9 2069
死守一世寂寞
死守一世寂寞 2020-12-05 05:25

Someone know how to add a another datasource in hibernate configuration and how to configure Spring to that datasource its autoinject in my respective DAO?

This is m

相关标签:
9条回答
  • 2020-12-05 06:15

    I had another way to offer which works only with java classes and annotations. Check it out at this SO post. https://stackoverflow.com/a/61232164/5681666

    0 讨论(0)
  • 2020-12-05 06:18

    I'll add my example too. Maybe it will be usefull for another situations like getting data from one database and writing it to another with EntityManager.

    So my applicationContext.xml:

    <context:annotation-config />
    <tx:annotation-driven transaction-manager="transactionManager" />
    
    <!-- Connection 1 -->
    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="persistenceUnitName" value="unitRemote" />
      <property name="packagesToScan" value="business.domain" />
    </bean>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
      <property name="url" value="jdbc:oracle:thin:@remote_host:1521:xe" />
      <property name="username" value="USER" />
      <property name="password" value="PASSWORD" />
    </bean>
    
    
    <!-- Connection 2 -->
    
    <bean id="transactionManagerLocal" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactoryLocal" />
    </bean>
    
    <bean id="entityManagerFactoryLocal" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSourceLocal" />
      <property name="persistenceUnitName" value="unitLocal" />
      <property name="packagesToScan" value="local.business.domain" />
    </bean>
    
    <bean id="dataSourceLocal" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
      <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
      <property name="username" value="USER_LOC" />
      <property name="password" value="PASSWORD_LOC" />
    </bean>
    

    Package business.domain has entity classes for remote connection.

    Package local.business.domain has entities for local connection.

    Entities have annotation mappings for columns and relations.

    Then 2 DAO classes:

    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;
    
    @Repository
    public class RemoteDao {
    
      @PersistenceContext(unitName="unitRemote")
      protected EntityManager em;
    
      // ...
    
    }
    

    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;
    
    @Repository
    @Transactional(value="transactionManagerLocal")
    public class LocalDao {
    
      @PersistenceContext(unitName="unitLocal")
      protected EntityManager em;
    
      // ...
    
    }
    

    With this I could use @Autowired to inject DAOs in my JUnit test:

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "/config/applicationContext.xml" })
    public class DaoTest {
    
      @Autowired
      private RemoteDao remoteDao;
    
      @Autowired
      private LocalDao localDao;
    
    
      @Test
      public void daoTest(){
        Entity entity = remoteDao.find(id);
        localDao.persist(entity);
      }
    
    }
    

    So here it's possible to use DAOs without service if this is needed for simple applications, tests or DB migration scripts.

    0 讨论(0)
  • 2020-12-05 06:21

    ok. I find another solution, and that is using the same method like this: add another dataSource and SessionFactory, next in the method of DAO that injject the sessionFactory add the @Qualifier anottation withe the property of the sessionFactory required, like this:

      @Autowired 
        public ProgramaSgteDAOHibernate(@Qualifier("sessionFactory3") SessionFactory sessionFactory) { 
         super(sessionFactory); 
        }
    
    0 讨论(0)
提交回复
热议问题