Thursday, August 7, 2008

LazyInitializationException

The Hibernate session is a runtime object that allows an application to create, read, update and delete entities in the data store. As long as the session is "open", these entities are attached to the session and you can navigate from one entity to another using relationships between them. If a related entity is not yet in memory, Hibernate will automatically pull it in for you (this is called "lazy loading"). However as soon you close the Hibernate session, the entities that exist in memory are considered to be "detached"; i.e. Hibernate no longer knows about them. You are free to hold references to such entities, but Hibernate will no longer pull in associated entities if they don't exist in memory already. If you accidentally try to access such associated entities, you will get a Hibernate LazyInitializationException.

No comments: