Java Hibernate更新对象最佳实践

Java Hibernate更新对象最佳实践,java,hibernate,Java,Hibernate,例如,我有这些简化的类: class Employer { int employerId; String name; Department department; } class Department { int id; String name; } 在hibernatechange雇主部门进行更新的推荐方式有哪些 做物体的方法 Employer e = employerDao.getById(id); e.setDepartment(new Depa

例如,我有这些简化的类:

class Employer {
    int employerId;
    String name;
    Department department;
}

class Department {
    int id;
    String name;
}
在hibernatechange雇主部门进行更新的推荐方式有哪些

做物体的方法

Employer e = employerDao.getById(id);
e.setDepartment(new Department(newIdDept));
dao.save(e)
或使用类似于更新hql的sql方式编写dao函数:

update set idDepartment=:newId where employerId=:id
对于它们,只需调用dao.updateDepartmentemployerId,newDptId

您应该使用SaveOrUpdate方法。只要需要,此方法就会在上调用save或update。如果数据存在于数据库中,则执行更新查询。

使用saveOrUpdate而不是save有什么好处?据猜测,它会影响索引插入、分区、触发器,从而影响性能;对吗?