-->
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.  [ 5 posts ] 
Author Message
 Post subject: Should I try to avoid "one-to-many" ? (DESIGN ques
PostPosted: Thu Jun 23, 2005 7:59 am 
Newbie

Joined: Fri Apr 29, 2005 11:55 pm
Posts: 14
Location: China-Australia
I'v used hibernate mapping in one of my web application. I found 'one-to-many' mapping very handy as it represents most of my class relationships.

For instance, I've got class Jurisdiction, Location, Contact. Each jurisdiction has more than one locations and each location has more than one contacts. What I've done at the moment is:


class Jurisdiction{
List locations;
}

class Location{
List contacts;
}


In my hibernate mapping file, a one-to-many mapping is made between Jurisdiction and Location, as well as Location and Contact.

So if I set lazy to false, once I load a Jurisdiction object, all locations belong to this jurisdiction and all contacts belong to those locations are all loaded. Sometimes it's almost a whole dump of my database.

I'm just wondering if the one-to-many is a proper way to map this kind of relationship, but on the programming side, it's very handy, I can just get the root object, and all details of the sub objects are available in a list.

However, someone has suggested that I should not do that, if I need the locations of a jurisdiction, just do another query, don't make it as a list of Jurisdiction class.

I would be happy if someone can have a bit discussion of this, is the latter way I commented the general use of hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 10:49 am 
Newbie

Joined: Tue Nov 23, 2004 8:19 am
Posts: 12
Location: Santa Maria-RS, Brazil
zhirenshao,

It's very up to the application; Is your application separated in layers, I mean, is the bussiness layer in a different machine than your web layer? If not, you may use lazy loading. If so, you may create many methods to load the Jurisdiction: one to load just Jurisdiction, other for Jurisdiction + Location, etc. The client code should choose the most suitable for its use.

I prefer the second choice (eager loading) even for the first situation, because the performance is better (just one SQL query with outter join).


Top
 Profile  
 
 Post subject: Re
PostPosted: Thu Jun 23, 2005 6:20 pm 
Newbie

Joined: Fri Apr 29, 2005 11:55 pm
Posts: 14
Location: China-Australia
hi rgmoro
At the moment, it's not n-tier, it's running on a single application server, although the database(SQL Server 2000) is running on another machine in the LAN(is it called 2-tier or something?), all the front-end web flow is managed in Struts.

I think you're quite right, if I use the second option(eager loading), is this a typical situation where Spring comes in and help me to manage the beans?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 8:20 am 
Newbie

Joined: Tue Nov 23, 2004 8:19 am
Posts: 12
Location: Santa Maria-RS, Brazil
Sorry, I don't know details about Spring; I just use Struts.

Did you undertand my sugestion? Implement a method for each situation according to the interface, loading eagerly every data needed by your use case. So you will be able to separate application layers (web and business) into different machines, and hibernate won't need to lazy load anything (remember that on the web layer you won't have a connection to the database).


Top
 Profile  
 
 Post subject: Thank
PostPosted: Fri Jun 24, 2005 8:26 am 
Newbie

Joined: Fri Apr 29, 2005 11:55 pm
Posts: 14
Location: China-Australia
Thanks for replying. According to what you suggested, I created a Service interface which has the retrieval methods, and my struts will use this Service interface to get data when needed.


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