小东 2014-05-14 14:45
浏览 212
已采纳

spring3与hibernate4整合session问题

[color=red][b]问题描述:[/b][/color]
整合spring3与hibernate4时,发生错误:[u]nested exception is org.hibernate.HibernateException: No Session found for current thread[/u]

之后查找资料,添加[u]:thread[/u]属性,报错:org.hibernate.HibernateException: createQuery is not valid without active transaction,
查找资料说,删除上面我添加的那个属性,我被陷入死循环了。
[color=red][b]代码如下[/b][/color]

[b]配置文件:[/b]
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:config/jdbc.properties</value>
        </list>
    </property>
</bean>

<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
    <property name="driverClass">  
        <value>${mysql.driver}</value>
    </property> 
    <property name="jdbcUrl">  
        <value>${mysql.url}</value>  
    </property>
    <property name="user">
        <value>${mysql.userName}</value>
    </property>  
    <property name="password">
        <value>${mysql.password}</value>
    </property>
    <property name="acquireIncrement">
        <value>${c3p0.acquireIncrement}</value>
    </property>    
    <property name="initialPoolSize">
        <value>${c3p0.initialPoolSize}</value>
    </property>
    <property name="minPoolSize">
        <value>${c3p0.minPoolSize}</value>
    </property>
    <property name="maxPoolSize">
        <value>${c3p0.maxPoolSize}</value>
    </property>
    <property name="maxIdleTime">
        <value>${c3p0.maxIdleTime}</value>
    </property>
    <property name="idleConnectionTestPeriod">
        <value>${c3p0.idleConnectionTestPeriod}</value>
    </property>
    <property name="maxStatements">
        <value>${c3p0.maxStatements}</value>
    </property>
    <property name="numHelperThreads">
        <value>${c3p0.numHelperThreads}</value>
    </property>
    <property name="testConnectionOnCheckout">
        <value>${c3p0.testConnectionOnCheckout}</value>
    </property>
    <property name="preferredTestQuery">
        <value>${c3p0.preferredTestQuery}</value>
    </property>
</bean>

<!-- Session 工厂配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />     
    <property name="packagesToScan">
        <list>  
            <value>com..entity</value>  
        </list> 
    </property>  
    <property name="hibernateProperties">         
        <props>          
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>          
            <prop key="hibernate.show_sql">true</prop>  
            <!-- 解决no session found -->
            <prop key="hibernate.current_session_context_class">thread</prop> 
        </props>     
    </property> 
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
    <property name="sessionFactory" ref="sessionFactory"/>  
</bean> 

<!-- 配置事物管理器,在*ServiceImpl里写@Transactional就可以启用事物管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />

<tx:advice id="txAdvice" transaction-manager="transactionManager">  
    <tx:attributes>  
        <!-- 方法对应的传播属性 -->  
        <tx:method name="save*" propagation="REQUIRED" />  
        <tx:method name="add*" propagation="REQUIRED" />  
        <tx:method name="create*" propagation="REQUIRED" />  
        <tx:method name="insert*" propagation="REQUIRED" />  
        <tx:method name="update*" propagation="REQUIRED" />  
        <tx:method name="merge*" propagation="REQUIRED" />  
        <tx:method name="del*" propagation="REQUIRED" />  
        <tx:method name="remove*" propagation="REQUIRED" />  
        <tx:method name="put*" propagation="REQUIRED" />  
        <tx:method name="use*" propagation="REQUIRED"/>  
        <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="count*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="list*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="*" read-only="true" />  
    </tx:attributes>  
</tx:advice>  

<!-- 事务控制位置,一般在业务层service -->  
<aop:config>  
    <aop:pointcut id="txPointcut" expression="execution(* com..service..*.*(..))" />  
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>  
</aop:config>

[/code]

[b]DAO[/b]
[code="java"]
package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseDao {
@Autowired
private SessionFactory sessionFactory;

public Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
}

}

[/code]

[code="java"]
package com.test.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.test.BaseDao;
import com.test.dao.IUserDao;
import com.test.entity.User;

@Repository
public class UserDaoImpl extends BaseDao implements IUserDao{

@SuppressWarnings("unchecked")
@Override
public List<User> findAllUsers() {
    String hql = "FROM User"; 
    Query query = getCurrentSession().createQuery(hql);  
    return query.list();  
}

}
[/code]

各层次的ioc都OK的,问题出在哪里,或者解答下原因也可以,我自己查找资料,谢谢了。

  • 写回答

7条回答 默认 最新

  • lovejiegirl1 2014-05-15 00:06
    关注

    在web.xml配置

    <filter>
        <filter-name>lazyLoadingFilter</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>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>lazyLoadingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)