我在Play/Scala中有兩個Hibernate類,Group
和Item
,其中一個組可能有很多項。我需要的是從組中刪除所有項目。我嘗試刪除這樣的:在Play Scala +休眠中發生ConcurrentModificationException
val group = session.get(classOf[Group],groupCode)
val it = group.items.iterator
while(it.hasNext) {
val i = it.next
group.items.remove(i)
}
第一次迭代工作正常,但在第二次迭代,我得到ConcurrentModificationException
。
這是什麼異常以及如何解決這個問題?
'it.remove'的作品,但是當我保存父對象時,我得到了Hibernate拋出的SQL異常。在SQL日誌中,我看到它試圖更新項目表而不是刪除行。由於它嘗試使用null設置列,因此JDBC會引發錯誤。這是日誌條目:'update items set group_sk = null where group_sk =?'。爲什麼它會嘗試更新而不是刪除? – ps0604
從group.items列表中刪除項目只會刪除項目和組之間的連接 - 它不會刪除項目對象本身。如果你想刪除Item對象,你需要做session.delete(i) – mateuszlo