标题: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;
}
}