Hibernate 3.0
MySQL
I have the following mind numbingly simple code:
Code:
HibHelper.beginTransaction();
Session s = HibHelper.getSession();
Criteria c2 = s.createCriteria("core.User");
List l = c2.list();
File f2 = new File("c:/temp/encode.xml");
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(f2)));
for (int x = 0; x < l.size(); x++) {
Object o = l.get(x);
encoder.writeObject(o);
}
encoder.close();
} catch (Exception e) {
Log.error(e);
}
The User Object has within it a collection of Roles.
When I try to run the above code though, I get:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection (core.User.roles) - no session or session was closed
The session though wasn't cloed. In fact it's a thread-local session so I never close it at all. So what am I missing here? Why am I seeing this error and how can I prevent it?