Hibernate using multiple databases

后端 未结 9 2108
死守一世寂寞
死守一世寂寞 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:13

    I had this same problem. I have solved this by creating: applicationContext.xml

    
    
        
    
    
    
    
        
        
        
        
    
    
        
        
        
            
                ${mySql.dialect}
            
        
        
            classpath:hibernate.cfg.xml
        
    
    
    
        
    
    
    
    
    
        
        
        
        
    
    
        
        
        
            
                ${oracle.dialect}
            
        
        
            classpath:hibernate.cfg.xml
        
    
    
    
        
    
    

    in Dao injected sessionFactory with Qualifier annotation. In my case I had generic BaseEnity:

    public abstract class BaseEntityDAOImpl implements BaseEntityDAO {
    
    private Class persistentClass;
    @Autowired
    @Qualifier("oracleSessionFactory")
    SessionFactory sessionFactory;
    }
    

    and in service bean using annotation:

    @Service
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, value = "oracleTransactionManager")
    public class UserService {
    
    @Autowired
    private UserDAO dao;
    }
    

    Averythings wokrs fine.

提交回复
热议问题