public User getUserById(int user_id) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
Query query = session
.createQuery("from User u where u id='"+user_id+"'");
tx = session.beginTransaction();
tx.begin();// 保持数据原子性,出错了可以回滚
User u = (User) query.list().get(0);
tx.commit();
return u;
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
HibernateSessionFactory.closeSession();
session.close();
}
return null;
}
------解决方案--------------------------------------------------------
from User u where u id=
u id之间有个空格,显然是把字段名写错了。