boblisweer
2014-05-14 14:45 阅读 209
已采纳

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 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>
    
    点赞 评论 复制链接分享
  • leehuat leehuat 2014-05-14 15:36

    [code="java"]

    是否是这里配置错误?
    [/code]

    点赞 评论 复制链接分享
  • greemran 犀利的蛙 2014-05-14 16:57

    你加入了 注解的扫描了吗?
    <!-- 启动注解扫描 -->

    点赞 评论 复制链接分享
  • iteye_11009 iteye_11009 2014-05-14 18:58





    com..entity



    改成这样试试 加载这个包下的所有实体类




    com..entity*



    点赞 评论 复制链接分享
  • lovalina lovalina 2014-05-14 23:01

    没有获取到受事务管理的session

    点赞 评论 复制链接分享
  • panpan_xin panpan_xin 2014-05-15 20:54

    hibernate4获取session有一个限制,就是session必须在事务级别上才能获取到
    你可以在你的service加上事务配置就没有问题了
    例如
    [code="java"]



    /aop:config

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" />
            <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" />
            <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" />
            <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" />
            <tx:method name="list*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" />
        </tx:attributes>
    </tx:advice>[/code]
    
    点赞 评论 复制链接分享
  • isaac0702 isaac_y 2016-08-17 10:50

    方法有点问题
    public Session getSession() {
    Session session = null;
    try {
    session = sessionFactory.getCurrentSession();
    } catch (HibernateException he) {
    session = sessionFactory.openSession();
    }
    return session;
    }

    点赞 评论 复制链接分享

相关推荐