1. 程式人生 > >解決hibernate 刪除異常: deleted object would be re-saved by cascade (remove deleted object from associations)

解決hibernate 刪除異常: deleted object would be re-saved by cascade (remove deleted object from associations)

在hibernate 刪除關聯時會出現eleted object would be re-saved by cascade (remove deleted object from associations)的異常,結合別人的和自己的經驗通常有三種解決的方案:

方法1 刪除Set方的cascade:
方法2 解決關聯關係後,再刪除

    onside.getManys().remove(thisMany);   //在所關聯的一方的set中移走當前要刪除的物件
   
thisMany.setOne(null);                          //設定所對應的一方為空,解除它們之間的關係

   
manyDao.delete(thisMany);

方法3 在many-to-one方增加cascade 但值不能是none