-->
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: Fetching joined properties in native SQL query?
PostPosted: Tue Oct 04, 2005 10:02 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 10:42 am
Posts: 54
Hello,

I have two tables - TEAMS and PLAYERS. PLAYERS have foreign key referencing TEAMS.

These tables are mapped to entities:
Code:
class Team
{
  Long    id;
  String  name;
  Integer gamesWon;
 
  IList players;
}

class Player
{
  Long   id;
  String name;
  String surname;
}

where " IList players" is mapped as lazy loading.

When I want to fetch the team along with all the players, I use the following HQL query:
Code:
from Team t left join fetch t.players


But now, I want to do the same using native SQL, in order to inject some Oracle optimizer hints.

How to do this?

My tried SQL query is:
Code:
String sql = "select {t.*}, {p.*} from  Teams t, Players p where  p.team_id = t.id ";
IList players = session.createSQLQuery(sql, new string[]{"t", "p"},
   new Type[]{typeof(Team), typeof(Player)}).list()

But the result is a list of ArrayLists returned, within contains both Team objects and Player objects, while the "players" property of Team object is still not "Initialized".

It seems like the SQL query dosen't know how to build up the relationship between parent and children.

Is there anyway I can specify the relationship when using SQL query?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 10:11 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 10:42 am
Posts: 54
I found there is addJoin() in Hibernate 3.x.

Is it hard to port this over?

See this post on Hibernate forum:
http://forum.hibernate.org/viewtopic.ph ... tesqlquery


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.