hibernate4+spring3.1整合常见问题解决方法

转自:http://blog.163.com/luckcq@yeah/blog/static/1717477072012229323993/

由于目前网上很少有关于hibernate4+spring3.1的配置方法,为了让大家少走弯路,我总结了一下与大家分享。

hibernate4的改动较大只有spring3.1以上版本能够支持。这里简单介绍了hibernate4相对于hibernate3配置时出现的错误,只列举了问题和解决方法,详细原理如果大家感兴趣还是去自己搜吧,网上很多。

1、spring3.1去掉了HibernateDaoSupport类。hibernate4需要通过getCurrentSession()获取session。并且设置<propkey="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>(在hibernate3的时候是thread和jta)。

2、缓存设置改为<propkey="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>

<propkey="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

3、spring对hibernate的事务管理,不论是注解方式还是配置文件方式统一改为:

<beanid="txManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">

<propertyname="sessionFactory"><refbean="sessionFactory"/></property>

</bean>

4、getCurrentSession()事务会自动关闭,所以在有所jsp页面查询数据都会关闭session。要想在jsp查询数据库需要加入

org.springframework.orm.hibernate4.support.OpenSessionInViewFilter过滤器。

5、hibernate分页出现ResultSetmayonlybeaccessedinaforwarddirection

需要设置hibernate结果集滚动<propkey="jdbc.use_scrollable_resultset">false</prop>

相关推荐