0 0

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

hibernate:4.1.6
spring:4.0.1

 

工程启动没错,进入getAllUser页面获得数据库用户信息时报如下错误: 

 

严重: Servlet.service() for servlet springMVC threw exception

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:941)

at com.mlf.web.controller.dao.UserDAO.getAllUser(UserDAO.java:27)

at com.mlf.web.controller.service.UserManager.getAllUser(UserManager.java:28)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)

at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)

at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)

at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)

at $Proxy19.getAllUser(Unknown Source)

at com.mlf.web.controller.UserController.getAllUser(UserController.java:36)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)

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:617)

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:149)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

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:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)

at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)

at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)

at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1760)

at java.lang.Thread.run(Thread.java:619)

-----------------------------------------------------------------------------------------------------------------------------------------

hibernate.cfg.test.xml(com.mlf.web.controller.hibernate)

 

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="com.mlf.web.controller.entity.User"/>
    </session-factory>
</hibernate-configuration>

 -----------------------------------------------------------------------------------------------------------------------------------------

 

applicationContext.xml(com.mlf.web.controller.spring)

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
	<bean id="userDao" class="com.mlf.web.controller.dao.UserDAO">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="userManagerBase" class="com.mlf.web.controller.service.UserManager">
		<property name="userDao" ref="userDao"></property>
	</bean>
	
	<bean id="userManager" parent="transactionBese">
		<property name="target" ref="userManagerBase"></property>
	</bean>
	
</beans>

  -----------------------------------------------------------------------------------------------------------------------------------------

 

spring-annotation.xml(包:config)

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
		
	<!-- 扫描包 -->
	<context:component-scan base-package="com.mlf.web.controller"></context:component-scan>
	
	<!-- 开启注解 -->
	<mvc:annotation-driven/>
	
	<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
	
	<!-- 静态资源访问 -->
	<mvc:resources location="/img/" mapping="/img/**"/>
	<mvc:resources location="/js/" mapping="/js/**"/>


	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>	
	
	<!-- 配置文件上传解析器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"/>
		<!-- 文件最大值 -->
		<property name="maxUploadSize" value="10485760000"/>
		<!-- 缓存大小 -->
		<property name="maxInMemorySize" value="40960"/>
	</bean>
		
</beans>

   -----------------------------------------------------------------------------------------------------------------------------------------

 

spring-core.xml(包:config)

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
	<import resource="classpath:com/mlf/web/controller/spring/applicationContext.xml"/>
</beans>

    -----------------------------------------------------------------------------------------------------------------------------------------

 

spring-hibernate.xml(包:config)

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncodeing=utf-8"></property>
		<property name="username" value="root"></property>
		<property name="password" value="donny2014"></property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
				<!-- 自动更新 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- 在控制台打印SQL语句 -->
				<prop key="hibernate.show_sql">true</prop>
				<!-- 将SQL脚本进行格式化后再输出 -->
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
		<property name="configLocations">
			<list>
				<value>
					classpath:com/mlf/web/controller/hibernate/hibernate.cfg.test.xml
				</value>
			</list>
		</property>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 配置事务 -->
	<bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
		<property name="transactionManager" ref="transactionManager"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="get*">PROPAGATION_NEVER</prop>
			</props>
		</property>
	</bean>
	
</beans>

     -----------------------------------------------------------------------------------------------------------------------------------------

 

UserController.java(包:com.mlf.web.controller)

 

package com.mlf.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mlf.web.controller.entity.User;
import com.mlf.web.controller.service.IUserManager;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource(name="userManager")
	private IUserManager userManager;
	
	@RequestMapping("/toAddUser")
	public String toAddUser(){
		return "/addUser";
	}
	
	@RequestMapping("/addUser")
	public String addUser(User user){
		
		userManager.addUser(user);
		
		return "/success";
	}
	
	@RequestMapping("/getAllUser")
	public String getAllUser(HttpServletRequest request){
		List<User> user = userManager.getAllUser();
		
		request.setAttribute("user", user);
		
		return "/userManager";
	}
}

 


问题补充:<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:config/spring-*.xml</param-value>
  </context-param>
 
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
 
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:config/spring-*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
  <param-name>forceEncoding</param-name>
  <param-value>true</param-value>
  </init-param>
  </filter>
  <!-- encoding filter for jsp page -->
  <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
 
  <filter>
  <filter-name>openSession</filter-name>
  <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
 
  <filter-mapping>
  <filter-name>openSession</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>
2014年12月24日 10:51
  • 大小: 5.2 KB

2个答案 按时间排序 按投票排序

0 0

采纳的答案

没发userDao啊

在getAllUser方法上加Transactional注解

如果没有事务,刚开始的线程上下文是拿不到session的

2014年12月25日 09:35
0 0

把WEB XML发上来看看呗

2014年12月24日 13:42

相关推荐

    org.hibernate.HibernateException: No Hibernate Session bound to thread

    然而,在使用 Hibernate 进行数据库操作时,经常会遇到 "No Hibernate Session bound to thread" 的错误信息。本文将详细介绍该错误的解决方案。 错误原因 "No Hibernate Session bound to thread" 错误信息通常是...

    解决SpringDataJPA报错:org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null w

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/...

    Spring4.0+Hibernate4.0+Struts2.3整合案例

    2、报错:org.hibernate.HibernateException: No Session found for current thread 意思是必须在transcation.isActive()条件下才能执行, 可以解决办法是:当方法不需要事务支持的时候,使用 Session ...

    HIbernate4.3.6整合c3p0所需jar

    Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.connection.C3P0ConnectionProvider] at org.hibernate.engine.jdbc.connections.internal....

    jdbc+hibernate的jar包

    3. `org.hibernate.HibernateException`:Hibernate抛出的异常类。 4. `org.hibernate.Query`和`org.hibernate Criteria`:用于执行HQL(Hibernate Query Language)和Criteria API查询,比直接使用SQL更加灵活。 ...

    com.microsoft.sqlserver.jdbc.SQLServerException: 只进结果集不支持请求的操作 解决方案

    query.setHint("org.hibernate.fetchSize", 20); // 设置查询结果集大小 query.setFetchSize(20); List&lt;Game&gt; games = query.list(); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx....

    Hibernate+Proxool配置

    由于项目需求的需要,我们引入了连接池。...我们采用了Hibernate,所以可以考虑hibernate自带的连接池机制,但是发现效率不高,而且Hibernate也推荐使用c3p0或Proxool连接池,在我们的项目中采用了Proxool

    Hibernate配置常见错误

    错误表现:在Session关闭后尝试访问懒加载属性,抛出“org.hibernate.LazyInitializationException: could not initialize proxy - no Session”异常。 解决方案:理解并合理使用Open Session in View(OSIV)模式...

    hibernate测试时遇到的几个异常及解决方法汇总

    3. org.hibernate.HibernateException: No CurrentSessionContext configured! 该异常的解决方法是添加 hibernate-jpa-2.0-api-1.0.0.Final.jar。这是因为 Hibernate 需要配置 CurrentSessionContext,以便正确地...

    J2EE利用Hibernate采用B/S架构网页设计

    import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; /** * Configures and provides access to Hibernate sessions, tied to the * current ...

    spring_MVC源码

    18. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; 19. &lt;property name="dataSource" ref="dataSource" /&gt; 20. &lt;property name="hibernateProperties"&gt; 21. &lt;props&gt; 22...

    Hibernate源代码分析

    public org.hibernate.classic.Session getCurrentSession() throws HibernateException { if (currentSessionContext == null) { throw new HibernateException("No CurrentSessionContext configured!"); } ...

    Hibernate开发必备版

    import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; ``` 2. **类声明:** ```java public class HibernateUtil { ``` 3. **静态...

    学习hibernate第一章内容

    5. 异常体系:理解Hibernate的异常体系,如HibernateException、ConstraintViolationException等。 6. Session和Transaction:理解Session的生命周期和事务管理,以及它们在实际应用中的最佳实践。 本章作为...

    JAVA错误文档.pdf

    4. Hibernate框架错误:文档还包含了“org.hibernate.HibernateException: No Hibernate Session bound to thread”等错误信息,这是Hibernate框架常见的异常,表明在当前线程上没有绑定Hibernate Session。...

    hibernate错误汇总

    错误八:Exception in thread "main" org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: org.model.User 错误原因:这个错误表示在尝试获取主键为1的实体...

    Jbuilder里配置Hibernate

    &lt;property name="current_session_context_class"&gt;thread &lt;!-- Echo all executed SQL to stdout --&gt; &lt;property name="show_sql"&gt;true &lt;!-- Drop and re-create the database schema on startup --&gt; ...

    hibernate-release-5.4.25.Final_Hibernate5.4.25_hibernate所需jar包_源

    5. 异常处理:Hibernate将SQL异常转换为特定的HibernateException,便于理解和处理。 总之,Hibernate 5.4.25为Java开发者提供了强大的数据持久化解决方案,通过合理的jar包配置和应用,可以极大地提升开发效率,...

Global site tag (gtag.js) - Google Analytics