-->
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.  [ 2 posts ] 
Author Message
 Post subject: Map to Set of objects: Recommendations on how to model it
PostPosted: Tue Jan 11, 2005 6:36 pm 
Newbie

Joined: Fri Dec 10, 2004 6:58 pm
Posts: 4
Hi guys -

I have a model, which naturally feels like it wants to look like:

Monday -> [ foo, bar, baz ]
Tuesday -> [ x, y, z]

So, from the main object you could:

Set sites = foo.getSitesFromDay(Monday)

and then sites would contain a foo, bar, and baz.

Is there a nice clean way to do this? Or do you have to:

Monday -> HolderObject1
Tuesday -> HolderObject2

and then HolderObject1 just contains a Set of Sites?

I appreciate any words of wisdom!

Cheers,

D


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 7:05 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Sounds like a bi-directional relationship would do the trick.

For example:

public class Day {
private Set sites
public Set getSites() { return sites; }
public void setSites(Set sites) { this.sites = sites }
}

public class Site {
private Day day;
public Day getDay() { return day; }
public void setDay(Day day) {this.day = day;}

public Set getSitesFromDay() {
return getDay().getSites();
}
}

You had: foo.getSitesFromDay(Monday). It's much better to have the relationship back to Monday as part of the class. In my code, it would instead be:

foo.getSitesFromDay();

In fact, I'd actually name it something like getSiblings() so that the semantics of the name convey the object structure.

As for the Hibernate mapping, simply map the Day.getSites() as a Set (probably inverse=true) and Collection-one-to-many and map the Site.getDay() as a many-to-one. Very simple.


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