Hi,
I have a session facade method like this:
Code:
public Set getUserPublishers(Long userId) {
try {
User user = (User) HibernateUtil.getSession()
.get(User.class, userId);
if (user == null)
throw new IllegalArgumentException("No such user");
Hibernate.initialize(user.getPublishers());
return user.getPublishers();
} catch (HibernateException e) {
throw new InfrastructureException(e);
} finally {
HibernateUtil.closeSession();
}
}
But when I try to access publisher fields in client side (outside session), I get LazyInitializationException. Why isn't Set initialized? Is there something wrong with my mappings?
If I change to <class name="Publisher" lazy="false">, this code works. But how can I make it work with lazy? (I thought "class lazy" only affects many-to-one associations?)
Hibernate version: 2.1.8 Mapping documents: (Unimportant properties removed)
Code:
<class
name="User"
table="users"
dynamic-update="false"
dynamic-insert="false"
lazy="true"
>
<set
name="publishers"
table="publishers_users_link"
lazy="true"
inverse="false"
cascade="save-update"
sort="unsorted"
>
<key
column="user_id"
>
</key>
<many-to-many
class="Publisher"
column="publisher_id"
outer-join="auto"
/>
</set>
<class
name="Publisher"
table="publishers"
dynamic-update="false"
dynamic-insert="false"
lazy="true"
>
<set
name="users"
table="publishers_users_link"
lazy="true"
inverse="true"
cascade="save-update"
sort="unsorted"
>
<key
column="publisher_id"
>
</key>
<many-to-many
class="User"
column="user_id"
outer-join="auto"
/>
</set>