-->
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: bidirectional relation and delete from child problem
PostPosted: Thu Jan 07, 2010 2:07 pm 
Newbie

Joined: Fri Nov 06, 2009 8:54 am
Posts: 4
Hi all,

I've a problem mapping my objects. Let's imagine you have a product class and a category class.
Product class has a Category property and category class has a Product
So when I check Product.Category property I have the category linked to the product or Null value if no category is linked.
When I check the Category.Product property, I have the product linked to that category.
My example is quite bad because a category should contain many products but it's not what i want, i want a one-to-one relation.

If I delete a product, all is ok.
But when I want to delete a category, I have an error. I would like to make NHibernate delete the category from its table and to set Ref_IdCategory value to null for the product concerned by this deleted category.

Here is my mapping :

Product :
Code:
<class="product">
<many-to-one name="Category" column="Ref_IdCategory" class="Category" />
</class>


Category :
Code:
<class="Category">
<one-to-one name="Product" foreign-key="Ref_IdCategory" class="Product" />
</class>


Do you have an idea about the mistake I made ?
Thanks
BR


Top
 Profile  
 
 Post subject: Re: bidirectional relation and delete from child problem
PostPosted: Fri Jan 08, 2010 3:04 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
As long as you use/need a one-to-one hibernate can't do that. Hibernate will always delete objects, so just the one product that is loaded will be deleted. So you get a violation of the foreign key for the other products in the database that have the same category.

You can use a foreign key with "on delete cascade set null"

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: bidirectional relation and delete from child problem
PostPosted: Fri Jan 08, 2010 4:51 am 
Newbie

Joined: Fri Nov 06, 2009 8:54 am
Posts: 4
Hi Wolli

Thanks for your answer ! Could you please apply what you said to my little example please ? because I don't know where to do that and what I have to change.
Thanks


Top
 Profile  
 
 Post subject: Re: bidirectional relation and delete from child problem
PostPosted: Fri Jan 08, 2010 7:40 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You have to apply that change to the database (foreign key):

ALTER TABLE <table> WITH CHECK ADD CONSTRAINT <name> FOREIGN KEY(<column>)
REFERENCES <table> (<column>) ON DELETE SET NULL

_________________
--Wolfgang


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.