0

I have the following two entities:

1- Deal

@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;

While doing session.delete(deal) I am getting the below error

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

Please help.

Anand
  • 20,708
  • 48
  • 131
  • 198

2 Answers2

1

That probably means that the object you are trying to delete is a child of some other collection. Setting up cascade="all" for the object means that its children would be deleted/updated automatically unless they don't belong to some other parent.

you have to remove the object you are trying to delete from its parent collection.

Example:

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

and then you can do

session.delete(objectBeingDeleted);

Good Luck

Abdel
  • 582
  • 4
  • 6
  • can you please provide the code snippet as per my problem with what should be done before calling delete on parent object? This issue is getting on my nerves now. – Anand Sep 24 '12 at 11:45
  • is doing deal.getDealCheckList().remove(deal); right? or should i do is doing deal.getDealCheckList().clear();? – Anand Sep 24 '12 at 12:15
0

You have a DealCheckList object that still references this deal and need to remove it from there in Java before deleting the deal by a hibernate call.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
  • can you please provide the code snippet as per my problem with what should be done before calling delete on parent object? This issue is getting on my nerves now. – Anand Sep 24 '12 at 11:44
  • If you post a bit more of the code you've got around this, I can try. But it's probably reasonably close to what Abdel put in his answer. – Don Roby Sep 24 '12 at 11:56
  • is doing deal.getDealCheckList().remove(deal); right? or should i do is doing deal.getDealCheckList().clear();? – Anand Sep 24 '12 at 12:14
  • As every `DealCheckList` entry seems to have a reference back to the deal, you likely want the `clear()`. – Don Roby Sep 24 '12 at 13:24