Hibernate3.0和Hibernate4.0的区别

Hibernate4的改动较大只有spring3.1以上版本能够支持, Spring3.1取消了HibernateTemplate,因为Hibernate4的事务管理已经很好了,不用Spring再扩展了 。这里简单介绍了hibernate4相对于hibernate3配置时出现的错误, 只列举了问题和解决方法 

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

  2. 缓存设置改为<prop key=" hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop> 
                    <prop key=" hiber nate.cache.region.factory_class ">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

  3. Spring对hibernate的事务管理,不论是注解方式还是配置文件方式统一改为: 
    <bean id="txManager" class=" org.springframework.orm.hibernate4.HibernateTransactionManager " >    
    <property name="sessionFactory"><ref bean="sessionFactory"/> 
    </property>   
    </bean>  

  4. getCurrentSession()事务会自动关闭,所以在有所jsp页面查询数据都会关闭session。 要想在jsp查询数据库需要加入: 
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter过滤器。

  5. Hibernate分页出现 ResultSet may only be accessed in a forward direction  需要设置hibernate结果集滚动 
     <prop key=" jdbc.use_scrollable_resultset ">false</prop>

猜你喜欢

转载自blog.csdn.net/qq_31001889/article/details/52621359