Since I did not get any replies to my question, perhaps I am not being clear. I will now be more specific.
I have code similar to this:
Code:
Session session1 = sessionFactory.openSession() ;
Query query1 = session1.createQuery("from Orders o where o.userId=:userId");
query1.setString("userId", "SOMEONE");
query1.setCacheable(true);
List results1 = query1.list();
// now close the session
session.close();
// even though the session is closed this iteration has no problem,
// since the query returned initialized proxied Order objects
for (int i = 0; i < results1.length(); i++) {
Order o = (Order)results1.get(i);
System.out.println("order description = " + o.getDesc());
}
// but the second time does not work:
Session session2 = sessionFactory.openSession() ;
Query query2 = session2.createQuery("from Orders o where o.userId=:userId");
query2.setString("userId", "SOMEONE");
query2.setCacheable(true);
// will return the cached results of the first query
List results2 = query2.list();
// now close the session
session.close();
// this time closing the session IS a problem, and will result in the exception noted above
for (int i = 0; i < results2.length(); i++) {
Order o = (Order)results2.get(i);
System.out.println("order description = " + o.getDesc());
}
Note once again that the Order object is mapped to use a proxy, and the Desc field is not part of the PK so the proxy needs to be initialized in order to get the value.
So my question is, if the first query already loads proxied objects that are already initialized and places the already initialized objects into the second-level cache, why when the 2nd query retrieves the cached results do those objects lose the "initialized" status? Why is the first query different than the 2nd, that with the first query it doesn't matter if the session is closed or not but in the second query it does? Is this perhaps a bug?
(In case you are wondering, others on my team want the Hibernate session closed in the servlet before forwarding to the JSP. So I would put the resulting List in the http servlet request object and iterate over the elments in my JSP to display the results. I would hate in my servlets to have to iterate over the result set and call initialize() on every element every time I run a query that the results would be used in this fashion. It seems a bug. )
Thanks,
Daniel