例如,我有这些“简化”类:
class Employer {
int employerId;
String name;
Department department;
}
class Department {
int id;
String name;
}
这是在hibernate中进行更新的推荐方法(更改雇主部门)
做(对象方式)
Employer e = employerDao.getById(id);
e.setDepartment(new Department(newIdDept));
dao.save(e)
或用更新hql编写dao函数,如(sql方式):
update set idDepartment=:newId where employerId=:id
和他们一样,只需致电dao.updateDepartment(employerId, newDptId);
答案 0 :(得分:0)
您应该使用SaveOrUpdate()
方法。此方法在需要的时间调用save()
或update()
。如果数据存在于数据库中,则执行更新查询。