报错代码:
return people;
try { String hql = "select id,name,age,gender,address,crimerecord,idno,pic from escapedpeopletbl where idno=?"; SQLQuery query2 = sessionFactory.getCurrentSession().createSQLQuery(hql).addEntity(People.class); query2.setString(0, idno); People people2 = (People)query2.uniqueResult(); return people2; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return null;当运行sessionFactory.getCurrentSession()时候报错:“No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here ”解决方法:
我用的是spring MVC来实现的,而spring MVC在实现的过程当中需要两个配置文件,第一个配置文件名称通常叫做:applicationContext.xml,第二个配置文件是用来定义 Spring MVC的模板文件的。
我将 annotation定义事务Java代码
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
Java代码
@Transactional
注意:在service 中加@Transactional ,不加这个,也找不到事务,也会报这个错。
附Service层代码:
package com.baple.service;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.wy.android.dao.PeopleDao;import com.baple.model.People; @Transactionalpublic class PeopleServiceImpl implements PeopleService { @Autowired PeopleDao peopleDaoImpl; @Transactional(readOnly=true) public People get(String idno) { return peopleDaoImpl.get(idno); } @Transactional(readOnly=true) public String getString(String idno) { return peopleDaoImpl.getString(idno); }}参考链接:http://www.iteye.com/problems/73211