trubosun 2016-10-07 08:53 采纳率: 50%
浏览 1279
已采纳

Hibernate4 No Session found for current thread异常

标题:Hibernate4 No Session found for current thread与createQuery is not valid without active transaction异常
描述:
在项目程序运行时遇到了Hibernate4 No Session found for current thread异常,

图片说明
百度了一下
在hibernate的配置文件中加入了thread
配置并且处理业务逻辑的类上也加入了注解如下

package com.xiaonei.baseService;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class BaseServiceImpl implements BaseService {

@Resource
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

/**

  • @author sunlong
  • @param hql:传入的hql,可以带参数?
  • @param args: 问号对应的参数数组
    */
    @Override
    public List<?> getResult(String hql, Object[] args) {

    Session session =  sessionFactory.getCurrentSession();
    System.out.println("执行到创建Session");
    Query query = session.createQuery(hql);
    System.out.println("执行到Query");
    if(args!= null && args.length > 0){
        for(int i = 0;i<args.length;i++){
            query.setParameter(i, args[i]);
        }
    }
    
    return query.list();
    

    }

但是当我再次运行程序时,出现了createQuery is not valid without active transaction异常
图片说明

然后我就接着百度,解决方法是把hibernate配置文件中的thread
配置删除或者把值thread改成org.springframework.orm.hibernate3.SpringSessionContext
再次运行却又出现了org.springframework.orm.hibernate3.SpringSessionContext异常整了好久都没搞明白,这是为什么呀?

Spring 的配置文件
1.applicationContext.xml

<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <property name="driverClass" value="${jdbc.driverClass}"></property>
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

    <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
    <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>


<!-- 配置 SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    <property name="mappingLocations" value="classpath:com/xiaonei/domain/*.hbm.xml"></property>
</bean>

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


<!-- 2. 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true"/>
        <tx:method name="lastNameIsValid" read-only="true"/>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

<!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 -->
<aop:config>
    <aop:pointcut expression="execution(* com.xiaonei.service.*.*(..))" id="txPointcut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

<!-- 自动装配具有注解的属性 -->
<context:component-scan base-package="com.xiaonei.service"></context:component-scan>


2.applicationContextbeans.xml

<bean id="countryService" class="com.xiaonei.service.serviceImpl.CountryServiceImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="provinceService" class="com.xiaonei.service.serviceImpl.ProvinceServiceImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="universityService" class="com.xiaonei.service.serviceImpl.UniversityServiceImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


    <bean id="registerAction" class="com.xiaonei.action.RegisterAction">
<property name="countryService" ref="countryService"></property>
<property name="provinceService" ref="provinceService"></property>
<property name="universityService" ref="universityService"></property>
</bean>

baseServiceImpl.java
package com.xiaonei.service.baseService;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public abstract class BaseServiceImpl implements BaseService {

@Resource
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

/**

  • @author sunlong
  • @param hql:传入的hql,可以带参数?
  • @param args: 问号对应的参数数组
    */

    @Transactional
    

    public List<?> getResult(String hql, Object[] args) {
    System.out.println("执行了getResult方法");
    Query query = sessionFactory.getCurrentSession().createQuery(hql);
    System.out.println("执行到createQuery方法");
    if(args!= null && args.length > 0){
    for(int i = 0;i<args.length;i++){
    query.setParameter(i, args[i]);
    }
    }

    return query.list();
    

    }

    @Override
    public void save(Object obj) {
    // TODO Auto-generated method stub

    }

    @Override
    public void delete(Object obj) {
    // TODO Auto-generated method stub

    }

    @Override
    public void update(Object obj) {
    // TODO Auto-generated method stub

    }

    @Override
    public Object findById(Class<?> calzz, Integer id) {
    // TODO Auto-generated method stub
    return null;
    }

}

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2016-10-07 16:19
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误