hi,
I have following mapping file:
Code:
<hibernate-mapping package="com.skopco.pronocl.data">
<class name="Team" table="TEAM">
...
<set name="users" table="USERTEAM">
<key column="TEAM_ID"/>
<many-to-many column="USER_ID" class="User" />
</set>
...
Basically a team contains users.
Now, I'm loading all teams and for each team get all it's users:
Code:
List teams = dao.getTeams();
for (Iterator iterator = teams.iterator(); iterator.hasNext();)
{
Team t = (Team) iterator.next();
Set s = t.getUsers();
for (Iterator iter2 = s.iterator(); iter2.hasNext();)
{
User u = (User) iter2.next();
...
I'll stop right there because this already throws an exception:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.skopco.pronocl.data.Team.users, no session or session was closed
if I change the mapping file like this:
Code:
<set name="users" table="USERTEAM" [b]lazy="false"[/b]>
it works fine (obviously).
My question is, if I want to hang on to lazy loading (
teams are loaded many times, it's users are only required now and then) then how can I load the team's users explicitly?
I could create an HQL-query which return the users by team-id but I'm sure there a 'hibernate-solution' for this.
many thanks for any help!
Stijn