No Hibernate Session bound to thread...问题解决

报错代码:android

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

解决方法:session

  我用的是spring MVC来实现的,而spring MVC在实现的过程中须要两个配置文件,第一个配置文件名称一般叫作:applicationContext.xml,第二个配置文件是用来定义 Spring MVC的模板文件的。
我将 annotation定义事务
app

Java代码  收藏代码.net

  1. <!-- 使用annotation定义事务 -->  xml

  2. <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />  事务


的配置写在了 applicationContext.xml 文件中,所以我在 Service 层中的事物注解
get

Java代码  收藏代码it

  1. @Transactional  io


并无起做用。

只须要将 annotation定义事务 的配置写在第二个Spring的配置文件中,问题就解决了!!!
感谢回复个人兄弟们,之后你们配置的时候千万要注意才行啊!!!

注意:在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;
@Service
@Transactional
public 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

相关文章
相关标签/搜索