Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LazyInitializationException: Failed to lazily initialize a collectio

 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did and now it's fine.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic