在hibernate4中出现org.hibernate.HibernateException: No Session found for current thread

本文详细介绍了在使用Spring3+Hibernate4时遇到的NoSessionfoundforcurrentthread错误的解决方法,包括在web.xml中添加特定配置以及调整Junit测试时的事务设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring3+hibernate4出错解决方法如下:在web.xml添加如下配置即可

<!-- openSessionInView配置 -->

<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>*.action</url-pattern>

</filter-mapping>

如果用Junit测试的时候总是报No Session found for current thread,这个错误则把

<!-- 拦截器方式配置事物设置为开启事务 -->

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="upd*" propagation="REQUIRED" />

            <!--请注意这里 propagation="SUPPORTS"  改为REQUIRED  read-only="true" -->
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>

改为:

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="upd*" propagation="REQUIRED" />

            <!--请注意这里 propagation="SUPPORTS"  改为REQUIRED  read-only="true" -->
            <tx:method name="get*" propagation="REQUIRED" read-only="true"  />
            <tx:method name="find*" propagation="REQUIRED"  read-only="true" />
            <tx:method name="load*" propagation="REQUIRED" read-only="true"  />
            <tx:method name="search*" propagation="REQUIRED" read-only="true"  />
        </tx:attributes>
    </tx:advice>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值