-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with CascadeTypes
PostPosted: Sat Mar 21, 2009 12:10 pm 
Newbie

Joined: Sat Mar 14, 2009 10:48 am
Posts: 7
Hi,

I'm still getting used to Hibernate but am currently having a little bother with deleting an object in a many-to-many relationship.

The functionality I have between the two models is pretty basic. Lets just say the two models are Product and Category, so many products can have many categories and vice versa.

If I delete one product, I do not wish to delete the categories it references and vice versa. The two tables use an association table since its a many-to-many relationship.

I've tried several different CascadeTypes and none seem to work correctly.

Using cascadeType.MERGE produces the following error:

Cannot delete or update a parent row: a foreign key constraint fails

CascadeType.ALL deletes more than 1 row incorrectly (ie. a record in the same table if they share a common attribute)

Using NO cascadeType returns the following error:

org.hibernate.ObjectDeletedException: deleted entity passed to persist manyToMany

and CascadeType.PERSIST throws an exception also:

org.hibernate.TransientObjectException: object references an unsaved transient instance

Sorry for the long-winded explanation but if anyone could help with this, I'd more than appreciate it!

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 21, 2009 12:19 pm 
Newbie

Joined: Sat Mar 14, 2009 10:48 am
Posts: 7
I should also mention that if I specify the following on BOTH models:

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "products")

I get an error also:

java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`lportal/ten_category_product`, CONSTRAINT `FKEF22751352CAE5BF` FOREIGN KEY (`product_fk`) REFERENCES `ten_product` (`id`))


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 22, 2009 8:15 am 
Newbie

Joined: Sat Mar 14, 2009 10:48 am
Posts: 7
Dont wanna sound impatient guys, but I seriously need help with this asap :)

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 9:05 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
The best thing to do is put a CascadeType Persist, merge and save-update on the primary end of relation (the one with no mappedBy)

Specify mappedBy only on one side.
Quote:
Using cascadeType.MERGE produces the following error:

Cannot delete or update a parent row: a foreign key constraint fails

Please note that before deleting the object you will have to unlink all the many-to-many associations.

Something like:
Code:
item.removeFromAllCategories();
session.delete(item);

_________________
Regards,
Litty Preeth


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.