hibernate查询语句commit 的时候出现问题
为什么一执行到tx.commit() 就直接走到catch里不能提交呢?
控制台也出现了Hql的select语句了啊,大家帮忙看下
public static User findByName(String username) {
List list = null;
User user =null;
try {
session = HibernateUtil.currentSession(); //开启连接
tx = session.beginTransaction(); //开启事务
//String hqlStr= "select * from usertest as user where user.username= ' "+username+ " ' ";
Query q=session.createQuery( "from User as a where a.username=:name ");
q.setString( "name ",username);
list = q.list();
tx.commit();
if(list!=null&&list.size()> 0){
user=(User) list.get(0);
}
} catch (
HibernateException e) { //捕捉例外
e.printStackTrace();
tx.rollback();
}
return user;
}
------解决方案--------------------帮忙顶
------解决方案--------------------select或createQuery语句是不需要提交的!
------解决方案--------------------from User as a where a.username=:name
from User where username= '+username+ " ' ";
这样试试看吧
------解决方案--------------------因为你发送的是纯SQL语句,在hibernate3里面,要调用SQLQuery query = session.createSQLQuery(String sql);
query.addEntity(String str,XXX.class);
query.list();
不知道我说的对不对,希望高手指正!