-->
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: foreign key id
PostPosted: Mon Oct 03, 2005 7:49 am 
Beginner
Beginner

Joined: Wed Sep 14, 2005 9:11 am
Posts: 22
Hi,
Suppose I've got a many-to-one (let's use PLAYER->TEAM) relationship in a class I'm retrieving. I want the query to be LAZY, so it only queries the PLAYER table at first. I then want to grab the id of the TEAM and do something with that. However, I still don't want the TEAM table to be queried, because I just need the teamId (which I should already have). For now, I've just added a separate property (teamId) to the Player class. Is there a better way to do this in hibernate? Here's my hbm.xml in case this helps:

<class name="us.benanderson.Player"
table="player">
...
<property name="teamId" type="string" column="team_id"/>
<many-to-one name="team"
column="team_id"
class="us.benanderson.Team"/>

Thanks,
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 11:02 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I believe that is the only way that you can do it. Or you can have a query on a DAO that retrieves them for you.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 11:07 am 
Regular
Regular

Joined: Wed Feb 02, 2005 6:33 am
Posts: 70
Could use HQL to retrieve more than one object in the select clause, e.g.

Queries may return multiple objects and/or properties as an array of type Object[],

Code:
select mother, offspr, mate.name
from DomesticCat as mother
    inner join mother.mate as mate
    left outer join mother.kittens as offspr


or as a List,

Code:
select new list(mother, offspr, mate.name)
from DomesticCat as mother
    inner join mother.mate as mate
    left outer join mother.kittens as offspr


or as an actual typesafe Java object,

Code:
select new Family(mother, mate, offspr)
from DomesticCat as mother
    join mother.mate as mate
    left join mother.kittens as offspr


assuming that the class Family has an appropriate constructor.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 11:29 am 
Beginner
Beginner

Joined: Mon Aug 16, 2004 6:15 am
Posts: 24
My experience is currently with Hibernate 2, so apologies if it has changed for version 3.

Have you tried accessing the id property of the Team object that is retrieved with the Player? Hibernate is smart enough to know that this data is already available, so won't actually query the database until you call the accessor for a non-key field.

Code:
/* Some query to get Player Objects here */
System.out.println(player.getTeam().getId());
System.out.println(player.getTeam().getNonKeyData());  <==  Query happens here


So you should be able to use your current model happily and just refer to your Team Id property, with no need to define a second property on the Player class.


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.