小东 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 vue2(标签-chrome|关键词-浏览器兼容)
  • ¥15 python网络流自动生成系统 医学领域
  • ¥15 sql查询仓库里都有什么
  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器