aspose file tools*
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 Java 8 in Action this week in the Java 8 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LazyInitializationException: Failed to lazily initialize a collectio
 
Similar Threads
Is it possible/practical to use lazy loading with servlets and JSPs?
how to "reattach" a serialized entity to a hibernate sesison?
failed to lazily initialize
LazyInitializationException illegal access to loading collection
failed to lazily initialize a collection - no session or session was closed