且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NoSuchMethodError: org.hibernate.SessionFactory.openSession

更新时间:2022-09-29 17:27:36

签:hibernate4.1 spring3.1 常见问题
严重: Servlet.service() for servlet dispatcher threw exception
java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
 at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:322)
 at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:256)
出问题喽....
没有这个方法?
哦...到hibernate4的时候,spring已经不再提供hibernateDaoSupport.getTemplate也不支持了.
就用hibernate4的session就可以了.
我把内容修改为了
直接使用注入的sessionFactory.openSession();来取得session.看看行不行...
 
找到篇好文章,我之前遇到的问题都在这都能找到.其实出现这些问题的关键就是hibernate4和hibernate3出现了session管理的变动.spring也作出相应的变动....
错误1:java.lang.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: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="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报错类似,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>









,如需转载请自行联系原作者