the pojos did not change, nor did the hbm.xml files
That's correct.
The configuration is no longer set at hibernate.cfg.xml - the Spring MVC FW loads it's own LocalSessionFactoryBean once I declared it in the XML files (Spring search for xmls as part of the initialization process).
You can choose to use Spring to configure Hibernate (but you could keep your hibernate.cfg.xml
).
So I created a separate xml file called spring-hibernate.xml, that has the DB definition, the session factory bean and a hibernate template. To complete it, the xml file also declare the location of the Dao and the hbm files. with hibernate template configured in the xml, there is a bean injection, therefore I changed the Dao files to add getter/setter to a hibernate template property. Then using this - the Dao class may create the db query.
Using Spring's HibernateTemplate
is one option, but you could also go template-less and just inject Hibernate's SessionFactory
and use sessionFactory.getCurrentSession()
.
This is actually the officially recommended approach for new projects, check out So should you still use Spring's HibernateTemplate and/or JpaTemplate?? and the javadoc of HibernateTemplate
.
Now my problem is - how to get access to the Dao bean instance created?
You inject it where needed (typically, in your services).
In my logic part, I get a hold of the bean as follows (...)
No, don't do this. This "service locator approach" actually defeats the point of a DI container like Spring. Configure Spring to inject your DAOs where needed.