wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes LazyInitializationException: Failed to lazily initialize a collectio Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "LazyInitializationException: Failed to lazily initialize a collectio" Watch "LazyInitializationException: Failed to lazily initialize a collectio" New topic
Author

LazyInitializationException: Failed to lazily initialize a collectio

John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
Hi,
I get this:

The thing is that I "read" this object from the database in previous (earlier in the code) session.
Now, I get this when I am doing and I am with in a session but a new one.
what should I do?

Thanks.
John Griffin
author
Greenhorn

Joined: Oct 24, 2008
Posts: 22
I assume that you have an association to another entity in the one that throws the lazy error.

The problem is that the entities in the associated collection only have their ID value present. This is because the default fetch type is LAZY. When you try to access one of the associated objects your app blows an error about lazy initialization because you are in a new session and this session knows nothing about the association.

To fix this the original association must be marked as fetch type EAGER but BE CAREFUL! This will cause all entites in the associated collection to be completely filled in. Monitor the SQL generated by the association and make sure that an outer join is generated. You do not want a SQL query generated for each of the associated objects!

Hope this helps.

John G
John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
did and now it's fine.
Thanks.
 
 
subject: LazyInitializationException: Failed to lazily initialize a collectio