-->
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: best practice relational mapping
PostPosted: Thu Jun 15, 2006 12:04 am 
Newbie

Joined: Wed May 31, 2006 6:55 pm
Posts: 2
Was wondering what the folks think about the best practice.
Maybe this is a more object model question than an ORM type of question.

Lets say there is a rule that says 1 Student has Many Cars.
But 1 Car can belong only to 1 Student.

so..

Student has a one-to-many relationship to Car

What are the merrits for this to be a bi-directional relationship?

ie..
if the Student.java

//
String name;
Set <Car> cars = new HashSet<Car>();
//getters and setters


Car.java
//
String brand;
Student student;
// getters and setters.

By doing this it is possible to declare the mappings and have in our Studen.hbm.xml

<set name="cars">
<key column="student_id"/>
<one-to-many class="Car"/>
</set>

In Car.hbm.xml

<many-to-one name="student" column="student_id"/>

----------------------------------------------------------------------

If the use case says we need to display all the cars associated for a particular student.. if we keep the bi-directionality (ie Student knows about its cars and a Car knows about its Student)
we could simply say.. studentX.getCars();
...
However if we keep a uni-directional relationship.. that is dropping the
Set <Car> cars = new HashSet<Car>(); from Student.java
and dropping this mapping from Student.hbm.xml
And if we had a DAO ran a method that would have some HQL like
("from Car c where c.student = student) and this returned a Collection of all the cars that belong to a particular student.
Thus from the 2 above strategies we are achieving the best result..
However...what is good practice?? to maintain bi-directionality when a domain object needs to know about its relationship to its children?
or to use uni-directionality and write hql to fetch collections of its parent children??

When do we need bidirectionality??? and when is not necessary??

Thanks..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 12:44 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Have a look at the ref docs, chapter 24, "Best Practices". In particular, the last item, "prefer bidirectional associations", may have some words of advice for you.

It's quite easy to find: go to the end of the ref docs. Voila.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 1:18 am 
Newbie

Joined: Wed May 31, 2006 6:55 pm
Posts: 2
Thanks..tenwit..
however the reference..though gives me an insight..it sounds like bidirectional is the better approach...
Could any one give me a reason why this is a best practice as oppose to using unidirectional only??
10nks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 2:43 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
They're equally good, except that it's more awkward to search using unidrection associations. If you're prepared to deal with the uglier query code, then it doesn't matter. That's why the ref docs just say to prefer bidirectional, rather than "don't use unidirectional".

In over 90% of cases, a lazily-loaded bidirectional association is just as efficient as the unidirectional version, and it's always more convenient.

_________________
Code tags are your friend. Know them and use them.


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.