報錯代碼: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
<!-- 使用annotation定義事務 --> xml
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> 事務
的配置寫在了 applicationContext.xml 文件中,所以我在 Service 層中的事物註解
get
@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