hibernate托管转持久,update,saveOrUpdate方法问题
public Teacher getTeacher(int id){Session session = HibernateSessionFactory.getSession();
Transaction ts = session.beginTransaction();
Teacher teacher = (Teacher) session.get(Teacher.class, id);
session.flush();
session.close();
return teacher;
}
public boolean updateTeacher(Teacher t){
Session session = HibernateSessionFactory.getSession();
Transaction ts = session.beginTransaction();
Teacher teacher = getTeacher(t.getTeaId());
System.out.println(teacher.getTeaId()+"*****"+teacher.getTeaName());
// Teacher teacher = (Teacher) session.get(Teacher.class, t.getTeaId());
teacher.setTeaUsername(t.getTeaUsername());
teacher.setTeaPassword(t.getTeaPassword());
teacher.setConfirmPassword(t.getConfirmPassword());
teacher.setRoleId(t.getRoleId());
teacher.setRoleName(t.getRoleName());
teacher.setTeaName(t.getTeaName());
teacher.setDepartmentId(t.getDepartmentId());
teacher.setDepartmentName(t.getDepartmentName());
teacher.setMajorId(t.getMajorId());
teacher.setMajorName(t.getMajorName());
teacher.setClassId(t.getClassId());
teacher.setClassName(t.getClassName());
teacher.setTeaEmail(t.getTeaEmail());
teacher.setTeaTelephone(t.getTeaTelephone());
teacher.setTeaPhone(t.getTeaPhone());
teacher.setTeaRemark(t.getTeaRemark());
session.update(teacher);
ts.commit();
session.flush();
session.close();
return true;
} hibernate 状态转换 --------------------编程问答-------------------- 忘记提问题了,问题是,为什么update()或是saveOrUpdate()都不成功! --------------------编程问答-------------------- Teacher teacher = getTeacher(t.getTeaId());换成
Teacher teacher = (Teacher) session.get(Teacher.class, t.getTeaId());
这样试下,我也不知道可以不 --------------------编程问答-------------------- 改了肯定可以,因为改了,就是把teacher纳入session缓存管理之中了,不用显示update都可以更新的。
现在就是,调用getTeacher(id)方法,是teacher对象为托管状态,从托管状态转为持久状态,用update或是saveOrUpdate跟新不行。有没有大神啊,告诉我一下啊,不胜感激!
补充:Java , Java EE