2010-03-18 72 views
2

我遇到一个问题,休眠那里试图删除一组实体我遇到以下错误时:提示解决的Hibernate/JPA EntityNotFoundException

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

这些都是不正常等等难以追踪,因为它们通常是由一个实体被删除,但没有从它所属的Collection中删除。

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

有没有人有任何提示解决这个特殊的例外?我特别寻找方法来确定可能是拥有的收藏品。

谢谢。

+2

如果您有CascadeType.ALL,您可以尝试删除CascadeType.PERSIST,看看会发生什么。 – 2010-03-18 07:17:59

+1

给你的映射/注释和代码 – Bozho 2010-03-18 08:53:23

+0

@Petar:+1是的,这个工程。不过,我更喜欢在那里有CascadeType.PERSIST,因为它使得更容易添加子实体。 – Damo 2010-03-18 21:46:17

回答

2

我建议做

getEntityManager().remove(freightInvoiceLine);

在最后一步。我认为这是一个很好的做法,首先将孩子从任何收藏中删除,然后实际删除它。它会在很多情况下节省头痛。

+0

我看不出它有任何区别,因为语句已排队并只在flush()或方法边界(如果自动刷新)上执行。 – Damo 2010-03-21 20:52:28

2

终于找到了它,而这正是令人沮丧的“找到列表”,我曾预料过的那种。

执行删除的代码扩展了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()方法,但更换后两者是没有区别的。

如果能够解释两者之间的差异,很乐意将其接受给别人。