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.  [ 7 posts ] 
Author Message
 Post subject: Got id of associated class - what's best way to use?
PostPosted: Mon Jan 16, 2006 2:43 pm 
Beginner
Beginner

Joined: Wed Sep 14, 2005 9:11 am
Posts: 22
Hi,
For my question assume the following:
A player is always on one team
A team consists of 1..* players

Suppose I have a team id and I want to create a new player. There is already a team associated with the id, but I don't need anything from team in this case except for the id. Don't worry about how I got the team id (in our case we're sticking it in the session). What's the best way to associate this player to the given team? At first I was doing a lookup of the team in hibernate using the team id... something like:

Team t = teamDAO.getObjectById(teamId);
Player p = new Player();
p.setTeam(t);
playerDAO.savePlayer(p);

I didn't really like this approach because the only reason I was doing the database select on team was to satisfy hibernate. Here's the more efficient way I've been using as of late:

Team t = new Team();
t.setId(teamId);
Player p = new Player();
p.setTeam(t);
playerDAO.savePlayer(p);

Are there any pitfalls to doing it this way? At least I'm not doing an unneccessary db select anymore. Is there a better way to do it? I know I could just map a property (teamId) to the Player class, but then I have to choose one of setTeam or setTeamId to not update the database.

Is anyone else facing such an issue?
Thanks,
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 10:48 pm 
Newbie

Joined: Wed Jan 11, 2006 5:21 am
Posts: 14
Can you post your hibernate mappings so we'll have a better picture?

Kind regards,
Roy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 11:47 pm 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
why dont you try a cascaded save .
something like team.addPlayer(p);

i'm much more comfortable with this approach . please comment on the
efficiency

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 12:27 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you're storing the ID somewhere, the best thing to do is to store the Team object instead. You're using an ORM now, not a JDBC connection: you have to think objects, not IDs.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 9:28 am 
Beginner
Beginner

Joined: Wed Sep 14, 2005 9:11 am
Posts: 22
I thought you would so that :-)
Ok, so what about this case? I've got a team page where I list all the players. Also on that page is a "create player" button which takes me to a page where I can add a player for that team. I don't need any team data on that page except for the id since I need to associate the new player with that team. In the spirit of using the session as minimally as possible, I decide to pass the team id as a request parameter. Can you accept this case as a valid time that I have the id and not the object?

-Ben


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 5:07 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The new Team(id) solution will work, if you're careful, though session.load(Team.class, id) is "nicer". And if you use the same session outside of the player-creation method, there's probably a reasonable change that the Team object is already in the cache, or will be reused before you release the session.

The principal pitfall is that the dummy Team object may get reattached to the session and saved, overwriting all the valid data in the DB. This is easy to avoid: ensure that there is no cascade from Player -> Team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 11:55 am 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
andersonbd27 wrote:
I thought you would so that :-)
Ok, so what about this case? I've got a team page where I list all the players. Also on that page is a "create player" button which takes me to a page where I can add a player for that team. I don't need any team data on that page except for the id since I need to associate the new player with that team. In the spirit of using the session as minimally as possible, I decide to pass the team id as a request parameter. Can you accept this case as a valid time that I have the id and not the object?

-Ben


yeah id is enough for the purpose . i prefer loading the team and adding the player to the collection . comments are welcome on the approach that i use .

_________________
sHeRiN
thanks for your ratings ...... :)


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