转载自:http://ligaosong.iteye.com/blog/1597427
错误1:java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
原因:spring的sessionfactory和transactionmanager与支持hibernate3时不同。
解决:
错误2:java.lang.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://jira.springsource.org/browse/SPR-9020
http://www.iteye.com/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="<a target="_blank" href="http://www.linuxso.com/command/find.html" style="padding: 0px; margin: 0px; background-image: none; border: none; color: #015f91;">find</a>*" read-only="true" propagation="REQUIRED"/><!--之前是NOT_SUPPORT-->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="up<a target="_blank" href="http://www.linuxso.com/command/date.html" style="padding: 0px; margin: 0px; background-image: none; border: none; color: #015f91;">date</a>*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="a<a target="_blank" href="http://www.linuxso.com/command/dd.html" style="padding: 0px; margin: 0px; background-image: none; border: none; color: #015f91;">dd</a>*" propagation="REQUIRED"/>
<!--默认其他方法都是REQUIRED-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
错误4:与错误3报错类似,java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg/hibe
rnate/classic/Session;
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(S
essionFactoryUtils.java:324) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.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.OpenSessionInViewFilter</filter-class>
</filter>