带有Hibernate的JPA EntityManager-查找和删除不会从数据库中删除数据 [英] JPA EntityManager with Hibernate - find and remove does not delete data from database

查看:210
本文介绍了带有Hibernate的JPA EntityManager-查找和删除不会从数据库中删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法使以下代码正常工作...

Having trouble getting the following code to work...

我有一个自动连接到该测试的JpaTransactionManager txManager.我知道ID为39的记录确实存在.交易结束时它仍然存在...

I've got a JpaTransactionManager txManager autowired into this test. I know record with ID 39 does exist. It still exists at the end of the transactions, too...

    TransactionStatus status = txManager.getTransaction(def);
    A a = mock(A.class);
    when(a.getId()).thenReturn(Long.valueOf(39));
    sut.delete(a);
    txManager.commit(status);

    status = txManager.getTransaction(def);
    a = sut.get(a.getId());
    txManager.commit(status);

    assertNull(a);

A中的代码:

public void delete(A a) {

    a = getEntityManager().find(A.class, a.getId());
    getEntityManager().remove(a);

}

上述assertNull检查始终失败有什么原因吗?无论如何我都无法从系统中删除对象-没有错误返回,并且报告的删除也没有问题. (顺便说一句,直接在HQL中运行查询确实会导致数据库更新...我只是无法使用JPA提供的delete方法使它正常工作.)

Is there any reason the above assertNull check always fails? I cannot delete the object from my system no matter what I do - no error returned, and no issue with the delete reported. (As an aside, running a query directly in HQL does result in an update of the database...I just can't get it to work using the delete method supplied using JPA...)

任何帮助表示赞赏

推荐答案

您应该看一下这些Hibernate类/方法:

You should take a look into these Hibernate classes/methods:

org/hibernate/engine/spi/ActionQueue.java executeActions(), unScheduleDeletion()
org/hibernate/event/internal/DefaultPersistEventListener.java onPersist()

我遇到了同样的问题-无法删除实体.在我的情况下,entityManager在其上下文"中有两个实体:父级有一个子级实体列表( cascade = CascadeType.ALL ),还有一个要删除的子级(从列表中).因此,当我尝试删除一个孩子时,父母仍然有一个链接,这导致Hibernate在刷新时"unScheduleDeletion".

I had the same problem - not being able to remove an entity. In my case, entityManager had two entities in its 'context': a parent with a list of children entities (cascade = CascadeType.ALL) and a child (from the list) to remove. So when I was trying to remove a child, parent still had a link to it, which was causing Hibernate to 'unScheduleDeletion' upon flushing.

这是解决方案:

  1. orphanRemoval = true添加到儿童集合中
  2. 创建方法deleteChild(Child child) {child.setParent(null); children.remove(child);}
  3. 使用此方法删除子项
  1. Add orphanRemoval = true to the collection of children
  2. Create method deleteChild(Child child) {child.setParent(null); children.remove(child);}
  3. Use this method to delete children

看起来另一个解决方案是删除级联,这样合并父实体不会导致保存其所有子级.不太确定(尚未选中).

Looks like another solution is to remove cascading, so that merging of parent entity wouldn't cause saving all its children. Not quite sure here (haven't checked).

据我所知,JPA规范描述了这种情况.

Also, as far as I remember, JPA spec describes this situation.

这篇关于带有Hibernate的JPA EntityManager-查找和删除不会从数据库中删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆