Spring framework3.2整合hibernate4.1报错:No Session found for current thread

问题描述: 出现No Session found for current thread  问题;

org.hibernate.HibernateException: No Session found for current thread
	org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
	org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
	dao.BaseDao.getSession(BaseDao.java:37)
	dao.BaseDao.save(BaseDao.java:53)
	dao.impl.IPDaoImpl.saveIP(IPDaoImpl.java:21)
	service.impl.IPServiceImpl.saveIP(IPServiceImpl.java:32)
	service.impl.IPServiceImpl$$FastClassByCGLIB$$4c6e6b59.invoke(<generated>)
	org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:627)
	service.impl.IPServiceImpl$$EnhancerByCGLIB$$66d9dec8.saveIP(<generated>)
	controller.CoreController.saveIp(CoreController.java:71)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:601)
	org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)




1. 检查配置文件是否出错:

***-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:content="http://www.springframework.org/schema/context"
       xmlns:comtent="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- 扫描dao.impl包下所有标注@Repository的DAO组件 -->
    <content:component-scan base-package="dao.impl" />
    <!-- 引入数据库配置文件 -->
    <comtent:property-placeholder location="jdbc.properties" />

    <bean id = "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="password" value="${jdbc.password}" />
        <property name="username" value="${jdbc.username}" />
    </bean>


    <bean name="sessionFactory" id = "sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
        <property name="dataSource" ref="dataSource" />
        <!-- 扫描com.hdu.cash.entity包下所有标注@Etity,@Table的实体类 -->
        <!-- 基于JSR 220的JPA注解 -->
        <property name="packagesToScan"  value="entity"/>

        <!-- 数据库方言 -->
        <property name="hibernateProperties">
            <props>
                <prop key="connection.autocommit">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <span style="color:#ff0000;"><prop key="current_session_context_class">thread</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop></span>
            </props>
        </property>
    </bean>

</beans>

注意红色字体的内容

***-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- service.impl包下所有标注@Service的服务组件 -->
    <context:component-scan base-package="service.impl"/>

    <bean id="txManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory" />

    <!--使用强大的切点表达式语言轻松定义目标方法-->
<span style="color:#ff0000;">    <aop:config  proxy-target-class="true">
        <!--通过aop定义事务增强切面-->
        <aop:pointcut id="serviceMethod"
                      expression="execution(* service.impl.*Service.*(..))" />
        <!--引用事务增强-->
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
    </aop:config></span>

    <!--事务增强-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!--事务属性定义-->
        <tx:attributes>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

   <span style="color:#ff0000;"> <tx:annotation-driven transaction-manager="txManager"/></span>

    <!-- 数据访问层配置 -->
    <import resource="IPPost-dao.xml" />
</beans>

2.如果配置文件没有错误 注意如下问题

serviceImpl 层添加如下代码,注意只能加在 实现类上,不能加在service接口层上,否则还是会报一样的错误。

@Service
@Transactional(propagation = Propagation.REQUIRED)
@TransactionConfiguration(transactionManager = "txManager", defaultRollback=true)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值