2010-03-18 75 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()方法,但更換後兩者是沒有區別的。

如果能夠解釋兩者之間的差異,很樂意將其接受給別人。