【问题标题】:Tips for resolving Hibernate/JPA EntityNotFoundException解决 Hibernate/JPA EntityNotFoundException 的提示
【发布时间】:2010-03-18 02:34:39
【问题描述】:

我遇到了 Hibernate 问题,在尝试删除一组实体时遇到以下错误:

javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.locuslive.odyssey.entity.FreightInvoiceLine#<null>]

这些通常并不难追踪,因为它们通常是由于实体被删除但未从它所属的集合中删除而引起的。

在这种情况下,我已经从我能想到的每个列表中删除了实体(这是一个复杂的数据模型)。我已经将 JBoss 登录到 Trace 中,我可以看到正在级联的集合。但是我似乎找不到包含我要删除的实体的集合。

有没有人有解决这个特殊异常的任何提示?我特别在寻找方法来识别可能拥有的集合。

谢谢。

【问题讨论】:

  • 如果您有 CascadeType.ALL,您可以尝试删除 CascadeType.PERSIST,看看会发生什么。
  • 给出你的映射/注释和代码
  • @Petar:+1 是的,这行得通。但是,我更喜欢在其中包含 CascadeType.PERSIST,因为它可以更轻松地添加子实体。

标签: java hibernate jsf jpa


【解决方案1】:

终于找到了,这正是我所期望的那种令人沮丧的“查找列表”。

执行删除的代码是扩展 Seam 的 EntityHome。

public class FreightInvoiceHome extends EntityHome<FreightInvoice> {
    public void deleteLine(FreightInvoiceLine freightInvoiceLine) {
      getEntityManager().remove(freightInvoiceLine);
      freightInvoiceLine.getShipInstrLineItem().getFreightInvoiceLines().remove(freightInvoiceLine);

      /* These next two statements are effectively performing the same action on the same FreightInvoice entity
       * If I use the first one then I get the exception. If I use the second one then all is ok.
       */
      getInstance().getFreightInvoiceLines().remove(freightInvoiceLine);
      //freightInvoiceLine.getFreightInvoice().getFreightInvoiceLines().remove(freightInvoiceLine);
    }
}

我曾怀疑这可能是由狡猾的 equals() / hashcode() 引起的,但替换两者后没有区别。

如果他们能解释两者之间的区别,很乐意将接受的内容更改为其他人。

【讨论】:

    【解决方案2】:

    我建议做

    getEntityManager().remove(freightInvoiceLine);

    作为最后一步。我认为这是一个很好的做法,首先从任何集合中删除孩子,然后实际删除它。在很多情况下,它会省去头疼的问题。

    【讨论】:

    • 我看不出它有什么不同,因为语句已排队并且仅在 flush() 或方法边界处执行(如果是自动刷新)。
    猜你喜欢
    • 2013-08-16
    • 1970-01-01
    • 2023-04-03
    • 2010-09-20
    • 1970-01-01
    • 2021-05-26
    • 2010-10-09
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多