Hibernate using multiple databases

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

    I had the same problem. I solved it like this:

    First of all, there should be different cfg.xml files for different databases. Then simply use Configuration object of the Hibernate whenever you want to connect to your second database.

    Configuration config = new Configuration().configure("");
    SessionFactory sessionFactory = config.buildSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    

    I found this here : http://www.coderanch.com/t/468821/ORM/java/If-hibernate-cfg-xml-has

    I am pretty sure that this can be extended to more than 2 databases. Hope this helps.

提交回复
热议问题