dononelson wrote:
And since the session is still open, the lazy instantiation works, because the object is still "attached" to the database.
This gets to the heart of my question so I'll address it first. According to the docs,
"lazy loading does not work outside of a database transaction. It doesn't matter if the Session is still open, it has no connection." So according to the docs I should get an exception when attempting to lazy load the property outside of a transaction, even though the session is open. If the documentation is wrong, no problem. If my understanding of the documentation is wrong, again no problem. The problem is that there appears to me to be either incorrect documentation, incorrect Hibernate behavior, or pilot error, and I want to know which it is.
dononelson wrote:
The first question I would ask is, why are you loading an object in a transaction, and then committing it? This is a query - there should be no updates needed.
There are no updates, but any database interaction requires a transaction, including loading. If I don't wrap the object load in a transaction it throws "org.hibernate.HibernateException: get is not valid without active transaction" which is correct and expected behavior. This is just test code by the way, and what I'm trying to do is "simulate" an action which is properly wrapped in a transaction and retrieves an object, and then does a "JSP render" which is not wrapped in a transaction and should thus throw an exception when it tries to lazy load a property.
Thank you for your ideas. I appreciate that you at least bothered to respond, but I don't think my question is answered yet. According to the documentation, the sample code should throw an exception since I'm accessing a lazy-loaded property outside of a transaction. Why isn't an exception being thrown?