-->
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.  [ 6 posts ] 
Author Message
 Post subject: Is bi-drectional necessary for a one-to-many ?
PostPosted: Mon Mar 29, 2004 1:33 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 7:18 pm
Posts: 26
If I have a Person class and Address class and a requirement that a Person can have many addressess (Set address in Person class), with no requirement saying that an address can belong to multiple people (1-M from Person to Address), do I still need to create a bi-directional mapping ?

I read the docs and it says something to the effect that this will need to be created ? Does that mean that now my Address class will need a Person instance in it ? I dont want that. I just want to add a bunch of addresses into the Person object and save Person and have the Addresses saved as well.

Any idea how best to do this in Hibernate ?

Pls help.

Thx,
Suman


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 5:01 pm 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
I guess it's possible to do it in some other way, but IMHO a bidirectional mapping with inverse="true" on the parent side is what one usually wants.

Think about how you do a one-to-many relationship in the database: On the "many" (children, or Address in your case) end, you have a column which is a foreign key to the "one" (parent, or Person in your case) end.

As you see, you still need to store the parent id with every child. So by making the mapping bidirectional, you don't lose any space or anything like that. OTOH you gain a great deal of convenience, since you can now retrieve the parent directly from java code without having to do a query.

If your usage scenario involves retrieving addresses, and not caring about who lives at the particular address, you might want to check out having the parent (Person) loaded lazily by proxy and possibly also disabling outer join fetching for the parent. That way the Person object won't be retrieved unless it is necessary.

Quote:
Does that mean that now my Address class will need a Person instance in it ? I dont want that. I just want to add a bunch of addresses into the Person object and save Person and have the Addresses saved as well.


Hopefully I don't come across as rude, but you are aware that in Java objects are call-by-reference, right?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 5:53 am 
Beginner
Beginner

Joined: Tue Mar 16, 2004 5:15 am
Posts: 33
Well doing it in a bi-directional way, would introduce two methods and one variable at parent side.

For example: Having an UML diagram and two objects and an uni-directional assoziation between those. How to model this? It is not an aggregation nor a composition, just a simple assoziation. Currently we are living using a bi-directional mapping while modeling an uni-directional but it feels wrong all the time.

What is the standard way to add an uni-directional assoziation?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 6:00 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
In your case, create a Person and an Address class, have a Set of addresses in the Person and map it as a collection of components (see the documentation).

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 3:43 pm 
Beginner
Beginner

Joined: Mon Nov 03, 2003 11:48 pm
Posts: 29
I look in the docs and see this:
The <component> element maps properties of a child object to columns of the table of a parent class.

But how to you have a unidirectional 1-M link between two tables?


Top
 Profile  
 
 Post subject: one-to-many
PostPosted: Thu Jul 01, 2004 11:48 am 
Newbie

Joined: Thu May 20, 2004 6:44 pm
Posts: 4
Location: USA
I get the impression from different posts that if the FK on the child side is nullable, bidirectinal association is not necessary. On the parent side one-to-many set inverse to false

_________________
Pranab Ghosh


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