Hello Hibernate gurus! I'm just getting started with Hibernate and am having trouble with the <set> attribute. I have a User class that has a PersistentSet called "roles", like so:
Code:
User.java
*********
public class User {
PersistentSet roles;
//...getter & setter
}
User.hbm.xml
************
<class name="BasicUser" table="users">
<set name="roles" table="roles">
<key column="user_id"/>
<element type="java.lang.String" column="user_role"/>
</set>
</class>
The problem is that when I try to create and save a user, I get a "org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed" exception. Here's the relevant code:
Code:
BasicUser basicUser = new BasicUser();
PersistentSet roles = new PersistentSet();
roles.add(Roles.USER);
basicUser.setRoles(roles);
basicUserDao.save(basicUser);
The exception is thrown at line 3, "roles.add(Roles.USER);". That seems like a really weird place to throw an error, so I presume I'm not instantiating PersistentSet correctly. The other constructors involve a SessionImplementor, but I can't find any documentation on where the SessionImplementor should come from. Any suggestions? Am I going about this entirely the wrong way? Thanks in advance!
Dane