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