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: Hibernate deletes although a constraint should forbid it
PostPosted: Mon Mar 08, 2010 12:29 pm 
Newbie

Joined: Mon Jul 20, 2009 5:09 pm
Posts: 16
Hello,

I've posted this issue already in German but maybe it is better to post it in English.

I have a constraint violation in my User-Right-Role example. When I delete a role that is associated to a user directly on my database, I get
SQL Error: 1451, SQLState: 23000
Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t_users_roles`, CONSTRAINT `FK_ur_roles_id` FOREIGN KEY (`roles_id`) REFERENCES `t_roles` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

This is what I want to get.
But when I delete it via Hibernate, the role and all associations are deleted. Why?

I've posted the code already here:
viewtopic.php?f=10&t=1003113

Thanks in anticipation,
iiMaestro


Top
 Profile  
 
 Post subject: Re: Hibernate deletes although a constraint should forbid it
PostPosted: Mon Mar 08, 2010 2:58 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hibernate is smart enough to know (because you have mapped this in your code) that it needs to delete the entries in the association table before it can delete the role. Turn on SQL logging and you will see that Hibernate issues several delete statements to the association tables before the role is deleted. To solve this you can map the association as an inverse relation. It seems like you currently have both sides as non-inverse which is not recommended. Use the "mappedBy" attribute as shown in the documentation examples to map an inverse assocation. http://docs.jboss.org/hibernate/stable/ ... ollections (scroll down a bit to the Many-to-many section)


Top
 Profile  
 
 Post subject: Re: Hibernate deletes although a constraint should forbid it
PostPosted: Tue Mar 09, 2010 5:21 am 
Newbie

Joined: Mon Jul 20, 2009 5:09 pm
Posts: 16
Thank you, nordborg.
You was right. When I insert the mappedBy clause everything works fine.

I used the Hibernate Tools for reverse engineering the mapping classes. That's why I did not use mappedBy. Maybe this comment can help also other newbies.

Thanks again.


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.