How to handle multiple database connections using session factories with Spring and Hibernate

后端 未结 1 1256
清酒与你
清酒与你 2021-01-23 20:28

I\'m really new to Hibernate and have been mulling over something for a while now. I have two databases that I have JNDI connection strings defined to in Tomcat\'s context.xml.

相关标签:
1条回答
  • 2021-01-23 20:56

    The HibernateTemplate constructor takes a SessionFactory as argument. I don't know what getHibernateTemplate() does in your code, but it should return a HibernateTemplate built with one of the SessionFactory beans you defined (either by declaring them in the spring context xml file, or by constructing them in Java from one of the injected session factories).

    Note that, as the documentation of HibernateTemplate says (in bold):

    As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate3 style of coding data access objects instead, based on SessionFactory.getCurrentSession().

    I would inject the session factory directly, and use the Hibernate API directly. HibernateTemplate doesn't bring much over the Hibernate API and often gets in the way, IMHO. (For example by not providing an equivalent to Query.uniqueResult()).

    0 讨论(0)
提交回复
热议问题