What technique do you use, when you need to access the lazy collections (those collections defined as lazy=true)?
Below is from hibernate document showing invalid access:
Code:
s = sessions.openSession();
Transaction tx = s.beginTransaction();
User u = (User) s.createQuery("from User u where u.name=:userName")
.setString("userName", userName).uniqueResult();
Map permissions = u.getPermissions();
tx.commit();
s.close();
Integer accessLevel = (Integer) permissions.get("accounts"); // Error!
I am thinking of doing something like this. Do you do the same thing? Please show us, how you do it.
Code:
s = sessions.openSession();
Transaction tx = s.beginTransaction();
User u = (User) s.createQuery("from User u where u.name=:userName")
.setString("userName", userName).uniqueResult();
Map permissions = u.getPermissions();
if (I_NEED_THEM_LAZY_COLLECTIONS){
Set keys = permissions.keySet(); //get all keys
for (Iterator i=keys.iterator;i.hasNext();){
Object justLoadingPermission = permissions.get((String)i.next());
}
}
tx.commit();
s.close();
Integer accessLevel = (Integer) permissions.get("accounts"); // Will this work?