Hibernate4在开发当中的一些改变

  • 格式:docx
  • 大小:17.83 KB
  • 文档页数:5

Hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了HibernateTemplate,因为Hibernate4的事务管理已经很好了,不用Spring再扩展了。

这里简单介绍了hibernate4相对于hibernate3配置时出现的错误,只列举了问题和解决方法,详细原理如果大家感兴趣还是去自己搜吧,网上很多。

1. Spring3.1去掉了HibernateDaoSupport类。

hibernate4需要通过getCurrentSession()获取session。

并且设置<propkey="hibernate.current_session_context_class">org.springframework.orm.hibe rnate4.SpringSessionContext</prop>(在hibernate3的时候是thread和jta)。

2. 缓存设置改为<propkey="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvi der</prop><propkey="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhC acheRegionFactory</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="e_scrollable_resultset">false</prop>--------------------------------------------------------------------找到篇好文章,我之前遇到的问题都在这都能找到。

其实出现这些问题的关键就是hibernate4和hibernate3出现了session管理的变动。

spring也作出相应的变动....错误1:ng.NoClassDefFoundError:org/hibernate/cache/CacheProvider原因:spring的sessionfactory和transactionmanager与支持hibernate3时不同。

解决:<bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"/>...</bean><bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager "><property name="sessionFactory" ref="sessionFactory"/></bean>错误2:ng.NoSuchMethodError:org.hibernate.SessionFactory.openSession ()Lorg/hibernate/classic/Session原因:hibernate4之后,spring31把HibernateDaoSupport去除,包括数据访问都不需要hibernatetemplate,这意味着dao需要改写,直接使用hibernate 的session和query接口。

解决:为了改写dao,足足花了一天时间,然后一个个接口进行单元测试,这是蛋疼的一个主要原因。

错误3:nested exception is org.hibernate.HibernateException: No Session found for current thread原因:发现一些bean无法获得当前session,需要把之前一些方法的事务从NOT_SUPPORT提升到required,readonly=true见https:///browse/SPR-9020,/topic/1120924解决:<tx:advice id="baseServiceAdvice"transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT--><tx:method name="find*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT--><tx:method name="save*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="remove*" propagation="REQUIRED"/><tx:method name="add*" propagation="REQUIRED"/><!--默认其他方法都是REQUIRED--><tx:method name="*"/></tx:attributes></tx:advice>错误4:与错误3报错类似,ng.NoSuchMethodError:org.hibernate.SessionFactory.openSession ()Lorg/hibernate/classic/Session;atorg.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:324)[spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]atorg.springframework.orm.hibernate3.SessionFactoryUtils.getSession(Ses sionFactoryUtils.java:202)[spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter原因:因为opensessioninview filter的问题,如果你的配置还是hibernate3,需要改为hibernate4<filter><filter-name>openSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionI nViewFilter</filter-class></filter>--------------------------------------------------------------------由于Hibernate4已经完全可以实现事务了,与Spring3.1中的hibernatedao,hibernateTemplete等有冲突,所以Spring3.1里已经不提供Hibernatedaosupport,HibernateTemplete了,只能用Hibernate原始的方式用session:Session session = sessionFactory.openSession();Session session = sessionFactory.getCurrentSession();在basedao里可以用注入的sessionFactory获取session.注意, 配置事务的时候必须将父类baseServiceImpl也配上,要不然会出现错误:No Session found for currentthread, 以前是不需要的SessionFactory.getCurrentSession()的后台实现是可拔插的。