简体   繁体   English

在同一会话中休眠多个对象更新

[英]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.

相关问题 Hibernate:在不同的会话中更新相同的对象 - Hibernate: update same object in different session Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save - Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save Hibernate会话方法更新对象 - Hibernate session method to update object Hibernate会话成功更新对象,但偶尔无法 - Hibernate session successfully update object, but occasionally not Hibernate:多个表,同一个对象 - Hibernate: Multiple tables, same object JPA Hibernate:具有相同标识符值的不同 object 已经与 Z21D6F40CFB5125082E45ZZA0E 关联 - JPA Hibernate : A different object with the same identifier value was already associated with the session Spring + Hibernate:具有相同标识符值的不同对象已经与会话相关联 - Spring + Hibernate : a different object with the same identifier value was already associated with the session Hibernate 错误:具有相同标识符值的不同 object 已与 session 相关联 - Hibernate Error: a different object with the same identifier value was already associated with the session Hibernate在多个数据库中保留相同的对象,相同的表 - Hibernate persist same object, same table in multiple databases Hibernate 错误:org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话相关联 - Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM