spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread

spring+hibernate整合:报错信息如下

org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
    at cn.myic.dao.impl.BaseDaoImpl.getEntityClassName(BaseDaoImpl.java:58)
    at cn.myic.dao.impl.BaseDaoImpl.findAll(BaseDaoImpl.java:86)
    at cn.myic.service.impl.BaseManagerImpl.findAll(BaseManagerImpl.java:39)
    at cn.myic.controller.CourseController.showAll(CourseController.java:107)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

解决方法:

1.在web.xml配置文件添加:

    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <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>

2.在applicationContext.xml文件中配置sessionFactory的hibernate信息下添加:

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>

<prop key="current_session_context_class">thread</prop>
时间: 2024-10-02 09:54:48

spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread的相关文章

分析 org.hibernate.HibernateException: No Session found for current thread

/**      *      * org.hibernate.HibernateException: No Session found for current thread      * 分析:getCurrentSession()和当前事务有关系      *      * Spring hibernate 事务的流程      *      * 1.在方法开始之前      *         ①.获取Session      *         ②.把Session 和当前线程绑定,这样

org.hibernate.HibernateException: No Session found for current thread

严重: Servlet.service() for servlet [annomvc] in context with path [/joyen-learning] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] with root causeorg.hibernate.Hib

hibernate在使用getCurrentSession时提示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) 大致问题:hibernate在处理sessoin SessionFactory的getCurrentSession并不能保证在没有当前Se

dubbo spring pom文件报错:提示no declaration can be found for element &#39;dubbo:service&#39;.

pom文件报错:The matching wildcard is strict, but no declaration can be found for  element 'dubbo:service'. 解决办法: 找到dubbo的jar包,解压后在META-INF文件夹下找到dubbo.xsd 在eclipse中点击windows-->preferrence-->xml-->xmlcatalog 选择add-->catalog entry  -->file system

spring pom文件报错:提示no declaration can be found for element &#39;dubbo:service&#39;.

转自:http://blog.csdn.net/happylife_haha/article/details/52755425 pom文件报错:The matching wildcard is strict, but no declaration can be found for  element 'dubbo:service'. 解决办法: 找到dubbo的jar包,解压后在META-INF文件夹下找到dubbo.xsd 在eclipse中点击windows-->preferrence-->

spring Boot启动报错Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes

spring boot 启动报错如下 org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure

SpringMVC4+Hibernate4运行报错Could not obtain transaction-synchronized Session for current thread

查了一下相关资料,整理如下: 原因:Hibernate4 No Session found for current thread原因 解决方法: 1.  在spring 配置文件中加入  程序代码 <tx:annotation-driven transaction-manager="transactionManager"/> 并且在处理业务逻辑的类上采用注解  程序代码 @Service public class CustomerServiceImpl implements

部署CM集群首次运行报错:Formatting the name directories of the current NameNode.

1. 报错提示 Formatting the name directories of the current NameNode. If the name directories are not empty, this is expected to fail. 2. 问题原因 之前的namenode格式化了,所以这次不能格式化,需要把原来格式化的删除. 3. 解决方法 1)在/dfs目录下有dn或者nn [[email protected] dfs]# ls /dfs/ nn snn 删除三台机器

SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped[......]

非常诡异的报错,信息如下:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select count(*) from User u where u.userName=? and u.userPassword=? ]Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped类似的报错信息的解决方法,