在Hibernate中删除父子对象的问题“已删除的对象将通过级联重新保存”

时间:2012-09-24 10:10:53

标签: java hibernate hibernate-annotations hibernate-cascade

我有以下两个实体:

1-交易

@OneToMany(cascade=CascadeType.ALL,mappedBy = "deal", fetch = FetchType.EAGER)
@Fetch( FetchMode.SELECT)
private List<DealCheckList> dealCheckList;

2- DealCheckList

@JoinColumn(name = "DEAL_ID", referencedColumnName = "DEAL_ID", insertable = false, updatable = false)
@ManyToOne(fetch = FetchType.LAZY)  
private Deal deal;

在执行session.delete(deal)时,我收到以下错误

Caused by: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations):

请帮忙。

2 个答案:

答案 0 :(得分:1)

这可能意味着您要删除的对象是某个其他集合的子对象。为对象设置cascade =“all”意味着它的子节点将被自动删除/更新,除非它们不属于其他父节点。

您必须从其父集合中删除您要删除的对象。

示例:

objectBeingDeleted.getParent().getChildren().remove(objectBeingDeleted); 

然后你可以做

session.delete(objectBeingDeleted);

祝你好运

答案 1 :(得分:0)

你有一个DealCheckList对象仍然引用这个交易,需要在Java中删除它,然后再通过hibernate调用删除交易。