Reputation: 267010
Can someone outline the steps required to get hibernate working with spring mvc.
I've seen EntityDao's that basically inherit from a GenericDAo.
The generic Dao has basic operations like GetAll, GetByID, Save, Delete, etc.
And inside their methods they use:
getHibernateTemplate
So basically the Session has to be wired up in a bean, and mysql settings have to be set.
I find the spring documentation to be a little confusing: http://static.springsource.org/spring/docs/3.0.0.RELEASE/spring-framework-reference/html/orm.html#orm-hibernate
Upvotes: 1
Views: 1197
Reputation: 4177
There is another way. If you don't want to use HibernateDaoSupport then you could directly inject SessionFactory into your DAO classes. This avoids tying you down to Spring classes.
Refer this for example - Spring Doc
This shows how to use Hibernate APIs directly.
Hope that helps.
Upvotes: 0
Reputation: 403481
The basic components are:
SessionFactory
. This is typically done by the LocalSessionFactoryBean
, as used in the example in the link you posted. This exposes a Spring-managed bean that implements the Hibernate SessionFactory
interface.SessionFactory
. In many cases, the simplest thing to do here is to extend the convenient HibernateDaoSupport
class, which has a sessionFactory
property. HibernateDaoSupport
proves a getHibernateTemplate()
method, which gets a Hibernate Session
from the SessionFactory
and wraps it in a HibernateTemplate
object, which provides various convenience methods for doing common Hibernate operations, and is generally more useful than the raw Session
interface.Using this pattern, there is very little direct interaction between application code and the Hibernate API itself, its mostly done though s Spring intermediate layer. Some would call this a good thing, others would rather Spring stayed out of the way. This is a perfectly good alternative - there's nothing to stop you injecting your bean with SessionFactory
and using the Hibernate API directly. The HibernateDaoSupport
and HibernateTemplate
classes are there purely as a convenience.
Upvotes: 3