Java 已删除实体传递给持久性异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2178304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:27:32  来源:igfitidea点击:

Deleted entity passed to persist exception

javahibernatejpa

提问by Cristian Boariu

I have this kind of entities:

我有这种实体:

Document | n .. to ..1 | DocumentType | 1 .. to .. n | PropertyType | 1 .. to .. n | DocumentProperty

文档 | n .. 到 ..1 | 文档类型 | 1 .. 到 .. n | 物业类型 | 1 .. 到 .. n | 文档属性

I simply try to remove a document like: entityManager.remove(document);

我只是尝试删除一个文档,如: entityManager.remove(document);

but an error is firing:

但是一个错误正在触发:

16:45:51,499 ERROR [[Seam Resource Servlet]] Servlet.service() for servlet Seam Resource Servlet threw exception javax.persistence.EntityNotFoundException: deleted entity passed to persist: [up.docstore.PropertyType#]

16:45:51,499 错误 [[Seam Resource Servlet]] servlet Seam Resource Servlet 的 Servlet.service() 抛出异常 javax.persistence.EntityNotFoundException:已删除实体传递给持久性:[up.docstore.PropertyType#]

The problem seems to come from here:

问题似乎来自这里:

@OneToMany(mappedBy = "documentType", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@ForeignKey(name = "FK_DOCUMENT_TYPE__PROPERTY_TYPE")
@Sort(type = SortType.NATURAL)
private SortedSet<PropertyType> propertyTypes = new TreeSet<PropertyType>();

If i remove CascadeType.PERSIST all it's working. But i need it there and also i need it EAGERLY.

如果我删除 CascadeType.PERSIST 一切正常。但我在那里需要它,我也急切地需要它。

Does anyone know other solution?

有谁知道其他解决方案?

Edit: removed DELETE_ORPHAN cascade, but still the same problem.

编辑:删除了 DELETE_ORPHAN 级联,但仍然存在同样的问题。

采纳答案by Cristian Boariu

Solution:

解决方案:

  • There was a CascadeType.REMOVE in a @ManyToOne relationship ! Removed it.
  • 在@ManyToOne 关系中有一个 CascadeType.REMOVE !删除了它。

Why this solution?

为什么是这个解决方案?

  • if you want to delete a child you SURELY do not want to delete its parent because there can be other children related to that parent.
  • 如果你想删除一个孩子,你肯定不想删除它的父级,因为可能有其他与该父级相关的子级。

回答by KLE

I see you are setting the cascadein two places : the @OneToManyand the @Cascade. I think this can be a problem, if one overrides the other ...

我看到你cascade在两个地方设置了: the@OneToMany和 the @Cascade。我认为这可能是一个问题,如果一个覆盖另一个......



The error you are reporting need some more context to be understandable. "Deleting an already deleted entity" involves clearly two operations .... You need to give details about the state before, the operations and the state afterwards (with "state", I mean the state in the database...).

您报告的错误需要更多上下文才能理解。“删除已删除的实体”显然涉及两个操作....您需要详细说明之前的状态、操作和之后的状态(“状态”是指数据库中的状态...)。

回答by GreenieMeanie

It seems like the Cascade options are somewhere making the Entity Manager think that this object or some other object in the chain needs to be persisted when you call em.remove(document). Need more specifics...

当您调用 em.remove(document) 时,似乎 Cascade 选项使实体管理器认为该对象或链中的其他对象需要持久化。需要更详细的...

回答by Bozho

I assume you have called remove()on an of type PropertyTypebefore. Call remove()only for the "root" entity, and remove the others with something like:

我假设您之前已经调用remove()过类型PropertyTyperemove()仅调用“根”实体,并使用以下内容删除其他实体:

document.getDocumentType().getPropertyTypes().remove(propertyType);

And retain the DELETE_ORPHAN

并保留 DELETE_ORPHAN

You can then, after verifying you haven't manually called remove()on other entities, try calling:

然后,您可以在确认您没有手动调用remove()其他实体后,尝试调用:

document = entityManager.merge(document);
entityManager.remove(document);

so that the EntityManagerreassociates the object with the session first.

以便EntityManager首先将对象与会话重新关联。