Simplified version of the object structure at hand:
Code:
class A
{
public int id;
public B bReference;
};
class B
{
public int id;
public String someData;
};
My simplified code:
Code:
Session session = HibernateUtil.getCurrentThreadSession();
try {
Query query = session.createQuery("from A as a");
searchResults = query.list();
} finally {
HibernateUtil.closeSession();
}
"searchResults" contains a collection of "A" instances (or proxy classes derived from "A"). However, when code tries to use the accessor to the bReference property, I get an exception:
javax.servlet.ServletException: javax.servlet.jsp.JspException: could not initialize proxy - the owning Session was closed
I assume this happens because the B instances weren't loaded and Hibernate is trying to load them when the accessor method gets called which is after the session is closed. Is this right?
I want Hibernate to preload the B instances and mappings as well. Does that make sense? How do I do that?
Or am I not supposed to close the session within a web app (JSF) and simply let each thread maintain an open session to Hibernate?
Thanks for any help!