例如,我有这些“简化”的类:
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)或者使用update hql编写dao函数(sql方式):
update set idDepartment=:newId where employerId=:id还有他们,就打电话给dao.updateDepartment(employerId, newDptId);
发布于 2015-04-05 02:53:25
您应该使用SaveOrUpdate()方法。每当需要时,此方法都会在上调用save()或update()。如果数据存在于数据库中,则执行更新查询。
https://stackoverflow.com/questions/29450155
复制相似问题