-->
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: a little problem
PostPosted: Wed Oct 15, 2003 10:58 am 
Newbie

Joined: Tue Oct 07, 2003 7:51 am
Posts: 7
i have to table players and teams
i have a bidirectional association between the two object

in team mapping file :

<set name="players" cascade=


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 11:08 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
According to the way Hibernate works, I believe that you can simply do:

Code:
Query q = session.createQuery("select team from " + Team.class.getName() + " as team");


This will return a list...which you can traverse:
Code:

List listResult = q.list();
for (int i = 0; i < listResult.size(); i++) {
    Team team = (Team) listResult.get(i);
    List players = team.getPlayers();
    for (int j = 0; i < players.size(); j++) {
        //you can access each player, and the team it came from here.
    }
}

This way you can go through all the matches.

Someone correct me if I'm wrong. :)

-G


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 1:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
Someone correct me if I'm wrong. :)

That probably the correct way to do it for most situations, as it allows you to maintain your object-level abstractions.

If you really need/want to be able to "iterator with both instances of Player and Team in each row", then try:
Code:
Query q = session.createQuery("select p, p.team from Player as p");

which will return you a result where each row is an Object[] where the first dimension is the player object and the second is the palyer's team.

Or even more succintly, just turn around the perspective of the object query:
Code:
Query q = session.createQuery("from Player as p");
for (Iterator itr = q.list().iterator(); itr.hasNext(); )
{
    final Player player = (Player)itr.next();
    final Team team = player.getTeam();
    ...
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 2:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
from Player p join fetch p.team

would be preferred ;)


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.