[英]Hibernate multiple object update in same session
Using Hibernate I need to update multiple objects by calling for eg. 使用Hibernate,我需要通过调用例如来更新多个对象。 updateDetails() function shown below: updateDetails()函数如下所示:
function updateDetails(){
Session session = this.getSessionFactory().openSession();
Employee emp=(Employee )session.load(Employee.class, empId);
emp.salary(2000);
Account acc=(Account)session.load(Account.class, accId);
account.setTotal(2000);
Transaction tx=session.beginTransaction();
session.update(emp);
session.update(acc);
tx.commit();
result=true;
session.close();
} }
what is the best way of doing this ? 最好的方法是什么?
does Updating multiple objects of same or different type in same session will cause any problem in hibernate? 在同一会话中更新多个相同或不同类型的对象会在休眠状态下引起任何问题吗?
Why not. 为什么不。 You can do that. 你可以做到的。 Hibernate allows. 休眠允许。
Since you are using Transaction management the answer is depends on your context. 由于您正在使用事务管理,因此答案取决于您的上下文。 If you want to save both Objects regardless of saving another make them update in individual transactions. 如果要保存两个对象而又不保存另一个,则使它们在单个事务中更新。
If you want to save the whole info successfully and want to revert everything if any of the update fail keep them in the current format. 如果您想成功保存整个信息,并且如果任何更新失败都想还原所有信息,则将其保留为当前格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.