-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: ConstraintViolationException when deleting parent entity
PostPosted: Tue Apr 02, 2013 11:19 am 
Newbie

Joined: Thu May 03, 2012 4:01 pm
Posts: 5
Hello everyone. I have the following scenario:

ParentA, ChildB tables / classes

ParentA has:

Code:
@OneToMany(mappedBy = "childId", cascade = { javax.persistence.CascadeType.PERSIST, javax.persistence.CascadeType.MERGE })
private Set<ChildB> bChildrens;


The table ParentA has no reference to ChildB. ChildB has a foreign key on ParentA's primary key.

What I would like is that when I delete a ParentA, all of the ChildBs that reference it are also deleted.

I've tried to modify the above annotation by changing it to javax.persistence.CascadeType.ALL or adding orphanRemoval=true, but I still get the constraint violated exception.

What did I do wrong?

Thanks as always,
TD


Top
 Profile  
 
 Post subject: Re: ConstraintViolationException when deleting parent entity
PostPosted: Thu Apr 04, 2013 4:11 am 
Newbie

Joined: Wed Mar 13, 2013 11:32 am
Posts: 16
You may need to look in you database for a FK constraint going from Parent to Child table and disable/remove it leaving only the FK constraint going from Child -> Parent.
I have experienced that when the database is created by hibernate one may experience that two foreign keys are created. One in each direction.


Top
 Profile  
 
 Post subject: Re: ConstraintViolationException when deleting parent entity
PostPosted: Thu Apr 04, 2013 4:58 am 
Newbie

Joined: Thu May 03, 2012 4:01 pm
Posts: 5
orwtech@gmail.com wrote:
You may need to look in you database for a FK constraint going from Parent to Child table and disable/remove it leaving only the FK constraint going from Child -> Parent.
I have experienced that when the database is created by hibernate one may experience that two foreign keys are created. One in each direction.


Thanks for the tip orwtech. I had the db already, and I checked that it only has one foreign key.
Now the issue is this:
Code:
@OneToMany(mappedBy = "childId", orphanRemoval=true cascade = { javax.persistence.CascadeType.PERSIST, javax.persistence.CascadeType.MERGE })
private Set<ChildB> bChildrens;


This makes a delete of a ParentA work, deleting all the orphaned childrens in the process, as expected. The problem now is that making an update to ParentA (not related to its children) triggers this exception now:

Code:
org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.