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?