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 b开发者_StackOverflowean, 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
The basic components are:
- Something to configure and create the Hibernate
SessionFactory
. This is typically done by theLocalSessionFactoryBean
, as used in the example in the link you posted. This exposes a Spring-managed bean that implements the HibernateSessionFactory
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 convenientHibernateDaoSupport
class, which has asessionFactory
property.HibernateDaoSupport
proves agetHibernateTemplate()
method, which gets a HibernateSession
from theSessionFactory
and wraps it in aHibernateTemplate
object, which provides various convenience methods for doing common Hibernate operations, and is generally more useful than the rawSession
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.
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.
精彩评论