-->
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.  [ 10 posts ] 
Author Message
 Post subject: one-to-one reference without the constraint
PostPosted: Fri Jan 04, 2008 10:27 pm 
Beginner
Beginner

Joined: Mon Sep 03, 2007 4:25 pm
Posts: 24
I am using the foreignkey implementation of one-to-one reference, where the Kit class is referencing Item class.

kit.hbm.xml
<many-to-one name="item" column="ITEMID" unique="true" />

this works nicely when navigating from kit to item. the problem is I really want this to be a loose coupling, where item does not know it is being referenced by kit, and can therefore be deleted anytime without worrying about this foreign key constraint.

is there any way in hibernate to implement that?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 1:34 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Define only a one-to-many set on the Item side, and nothing on the Kit side.
That results on a "pure" table for Items, and a Kits table with an additional ItemId column.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 3:32 pm 
Beginner
Beginner

Joined: Mon Sep 03, 2007 4:25 pm
Posts: 24
that's great. this achieves the end result that I want, but presents an inconvenience in implementation, because a modification needs to be done on item.hbm triggered by requirement on kit. similarly, any other entity who also wants to reference item, without item knowing about it, would cause a modification to item.hbm.

ideally, hibernate could have provide a means of achieving this by requiring entries on the side which created the motivation instead of affecting the passive partner. that would make cause-effect much more obvious.

But, yes, this approach is workable.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 3:52 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
lostindesert wrote:
that's great. this achieves the end result that I want, but presents an inconvenience in implementation, because a modification needs to be done on item.hbm triggered by requirement on kit. similarly, any other entity who also wants to reference item, without item knowing about it, would cause a modification to item.hbm.

ideally, hibernate could have provide a means of achieving this by requiring entries on the side which created the motivation instead of affecting the passive partner. that would make cause-effect much more obvious.


You are correct. It is weird that the side not affected, in terms of table columns, is the side holding the mapping information.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 5:15 pm 
Beginner
Beginner

Joined: Mon Sep 03, 2007 4:25 pm
Posts: 24
while trying to implement your suggestion, I notice you said to add a "set" using one-to-many. I believe this means I will have to have a Set<Item> defined in Kit class.

that would not be what I want, since I want Kit to refer to Item as getItem(), not getItemSet().toArray[0].

please explain further how your suggestion leads to one-to-one association.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 5:30 pm 
Beginner
Beginner

Joined: Mon Sep 03, 2007 4:25 pm
Posts: 24
after studying further, I realized that one-to-many is not a standalone tag, but must be inside a collection, like <set>.

This is clearly not a solution for what I want.

I know there is a way in sql to specify that foreign key column of child tables be set to null when the foreign key row is deleted. is there a way to implement this for <many-to-one>?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 06, 2008 12:53 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Conceptually, a foreign key can be a null value, which means that no row in the foreign table satisfies the key.

In the reference, the examples of one-to-many assume foreign keys with no-null values, but that is not mandatory.

And yes, you are correct, the tag that suits your problem is "set", "one-to-many" being an inner tag of it.


Quote:
is there a way to implement this for <many-to-one>?

Yes, many-to-one is a standalone tag, which you can specify in the "many" class and mapping object. Check section 7.2.1 of the reference for a simple, unidirectional example.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 08, 2008 4:57 am 
Beginner
Beginner

Joined: Mon Sep 03, 2007 4:25 pm
Posts: 24
you are not understanding my question at all.

I appreciate you trying to help, but given you are a newbie too, I don't think we are on the same wave length. hopefully, an experienced user would respond.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 9:50 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Thank you for your feedback, and good luck with your question.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject: Re: one-to-one reference without the constraint
PostPosted: Fri Jan 11, 2008 11:35 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I can think of some reasons why you can't get this working in HB and the most important one might be some problems this might cause for second level caching. I understand you want HB to go around and find out what it knows about an Item and update its cached objects (assuming it will update database references anyways) but to the best of my knowledge this is not how HB works at this time.

Your best solution might be to do this manually. Your requirement for isolating Item from Kit is perfectly understandable but you will have to set the reference to an item to null manually. This will require you to do more work. Meanwhile we can wait to see if anyone comes with a better idea.


Farzad-


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