No Session found for current thread

org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:990)

一直提示这个错误No Session found for current thread。

其他的各种配置都正确,最后在web.xml中发现了错误,添加如下代码:

<filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
      
         <filter-class>
            org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
         </filter-class>
         <!-- 指定在spring配置的sessionFactory -->
        <init-param>
            <param-name>H4TxManager</param-name>
            <param-value>mysqlSessionFactory</param-value>
            </init-param>
             <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->   
        <init-param>
         <param-name>singleSession</param-name>
        <param-value>true</param-value>
        </init-param>
    </filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


你可能感兴趣的:(java,thread,current)