这是我的代码
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;
}