简体   繁体   English

Hibernate:在不同的会话中更新相同的对象

[英]Hibernate: update same object in different session

I'm trying to do that but not working, can anyone suggest me what i'm wrong? 我试图这样做但没有工作,有谁能告诉我我错了什么? In the end I lose the first update 最后我失去了第一次更新

    EntityManager entityManager1 = JPAUtil.getEntityManagerFactory().createEntityManager();
    EntityManager entityManager2 = JPAUtil.getEntityManagerFactory().createEntityManager();

    Session session1 = (Session) entityManager1.getDelegate();
    Prova prova1 = session1.load(Prova.class, "020");

    Session session2 = (Session) entityManager2.getDelegate();
    Prova prova2 = session2.load(Prova.class, "020");

    prova2.setDes(prova2.getDes() + " 2");    
    prova1.setShortdes(prova1.getShortdes() + " 1");

    Transaction t2 = session2.beginTransaction();
    session2.update(prova2);
    t2.commit();
    session2.close();

    Transaction t1 = session1.beginTransaction();
    session1.update(prova1);
    t1.commit();
    session1.close();

Add the following code to fix: 添加以下代码进行修复:

    t2.commit();
    session2.close();
    session1.refresh(prova1)
    prova1.setShortdes(prova1.getShortdes() + " 1");

Transaction t1 = session1.beginTransaction();
    session1.update(prova1);
    t1.commit();

What is happening here is: 这里发生的事情是:

First you execute update on prova2 which results in des being updated. 首先,您在prova2上执行更新,这会导致des更新。

Then on prova1 shortdes is updated , but its detached state of des is the original prova state before prova2 was updated. 然后在prova1上更新shortdes ,但是它的分离状态des是prova2更新之前的原始prova状态。 The algorithm of update is such that it attempts to reattach prova1 to the persistent context and realizes that it differes from the persistent state in two fields so the first update is ommited and you end up with shortDes set but Des is back to its original state. 更新算法是这样的,它尝试将prova1重新连接到持久化上下文,并意识到它与两个字段中的持久状态不同,因此第一次更新被省略,最终得到了shortDes集,但Des又回到了原始状态。

Read more here https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate 在这里阅读更多内容https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate

As the previous answer you always need to check the version of object before saving. 作为上一个答案,您始终需要在保存之前检查对象的版本。 Hibernate or JPA has features called optimistic locking to fix this issue. Hibernate或JPA具有称为乐观锁定的功能来解决此问题。 Refer to the below code and link for more information. 有关更多信息,请参阅以下代码和链接。

@Entity
public class Employee 
{
  @Id
  @GeneratedValue
  private Integer id;
  @Version
  private long version;
  private String name;
}

When you create employee record the framework will automatically create version value. 创建员工记录时,框架将自动创建版本值。 At first lets assume the values are 首先让我们假设值是

[1, 0, "name1"] [1,0,“name1”]

On update it changes to 在更新时,它将更改为

[1, 1, "name2"] [1,1,“name2”]

On next update it changes to 在下次更新时,它将更改为

[1, 2, "name3"] [1,2,“name3”]

The framework will throw exception if you try to update data with wrong version 如果您尝试使用错误的版本更新数据,框架将抛出​​异常

Link has more information on this https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/optimistic-lock.html 链接有关于此https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/optimistic-lock.html的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 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 multiple object update in same 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 JPA Hibernate:具有相同标识符值的不同 object 已经与 Z21D6F40CFB5125082E45ZZA0E 关联 - JPA Hibernate : A different object with the same identifier value was already associated with the session Hibernate会话方法更新对象 - Hibernate session method to update object 休眠:在另一个会话中更新对象? - Hibernate: updating an object in a different Session? Hibernate 错误:org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话相关联 - Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session Hibernate Spring JPA会话:具有相同标识符值的另一个对象已与该会话关联 - Hibernate Spring JPA Session: A different object with the same identifier value was already associated with the session HIBERNATE抛出错误:“在更新对象时,“具有相同标识符值的另一个对象已与该会话关联”” - HIBERNATE Throwing ERROR:“a different object with the same identifier value was already associated with the session ”, while Updating the object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM