gpt4 book ai didi

java - 当我们在 hibernate 中删除实体时会检查版本吗?

转载 作者:行者123 更新时间:2023-11-30 03:28:28 25 4
gpt4 key购买 nike

我相信删除实体时会检查版本。但我刚刚读过……

In JPA, You have to merge the detached instance first and then remove the merged object (or, alternatively, get a reference with the same identifier, and remove that).

我对 Gavin King 在他关于 Hibernate 的书中建议的替代方法感到困惑

item1.setName("iphone")// this is a detached object

EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();

// this will give a proxy as we don't have any instance with this ID in
// persistence context. Now if this is a proxy it doesn't have any version.
// How hibernate decides whether to delete such an object or not.
Item item2 = em.getReference(Item.class, item1.getId());
em.remove(item2);

tx.commit();
em.close();

如果允许上述情况,是否意味着删除时不会检查版本?

最佳答案

为了删除一个对象,Hibernate 要求该对象处于持久状态。因此,Hibernate 首先获取对象 (SELECT),然后删除它 (DELETE)。

关于java - 当我们在 hibernate 中删除实体时会检查版本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29647559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com