Java 在hibernate中删除实体时是否检查版本?
我认为在删除实体时检查了该版本。但我刚刚读到 在JPA中, 您必须首先合并分离的实例,然后删除 合并对象(或者,获取具有相同 标识符,并将其删除) 我对加文·金在他关于冬眠的书中提出的替代方法感到困惑Java 在hibernate中删除实体时是否检查版本?,java,hibernate,jpa,Java,Hibernate,Jpa,我认为在删除实体时检查了该版本。但我刚刚读到 在JPA中, 您必须首先合并分离的实例,然后删除 合并对象(或者,获取具有相同 标识符,并将其删除) 我对加文·金在他关于冬眠的书中提出的替代方法感到困惑 item1.setName("iphone")// this is a detached object EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.beg
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首先获取对象(选择),然后将其删除(删除)。再进一步挖掘之后。。。 我同意Zeelous的观点,即必须将对象加载到持久性上下文中才能删除(代理也足够好)。原因是:你 可能已启用Hibernate侦听器,并且必须传递该对象 通过这些拦截器来完成其生命周期。如果删除中的行 直接访问数据库,拦截器将不会运行 要回答我的问题,它有两个选项: