views:

65

answers:

1

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

+3  A: 

The basic components are:

  • Something to configure and create the Hibernate 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.
  • Typically, you then have one or more DAO beans which are injected with the 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.

skaffman
great thanks. So my GenericDao will extend HibernateDaoSupport. And HibernateDaoSupport has to be wired up in a bean to set its SessionFactory.
Blankman