Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the solution for org.hibernate.LazyInitializationException?

 
Amr k. Saleh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

What is the solution for org.hibernate.LazyInitializationException without setting lazy to false, i don't think leaving session open is a good solution ?
i'm using xml mapping, please advice

thanks,
Amr Khaled
 
ntumba lobo
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have your persistent objects in your views to avoid the lazyInitializationException you have 2 options
a) eager loading: you make sure that before reaching your views all the persistent objects have loaded all the data they need
b) open session in session in vew pattern: you use a filter that let the hibernate session open while your views are rendered

Both are perfectly valid solutions it depends on your requirements and the amount of data your dealing with
 
Amr k. Saleh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ntumba for your reply, i thought about opening the session in view but it didn't sound a good solution for me because view tier and business tier will be coupled. what do you think ?
eager loading is not good because it brings everything.
 
ntumba lobo
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it's true that having persistent objects in view introduce some coupling but it is a fairly common practice because of the benefits it brings (clean and consice code).

If you are that bothered about the coupling you can introduce a layer of DTO that would store data from multiple persistent objects and that you would use
in your views. You dont have coupling anymore but we introduce a fair amount of boiler plate code. As always each approach has pros and cons,
weight both of them and decide for your scenario.

By the way eager loading doesnt mean you have to bring everything. It means you have to selectively load data needed by a specific view but not more.
This approach implies more upfront design and a more complex business layer as you have to create more business services that return different set of data for each use case. You dont have to care about that with open session in view pattern for example.

I hope that'll help you
 
Amr k. Saleh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ntumba, that answer is satisfied for me

I have an inquiry about the last paragraph, what I've understood that I'll not use hibernate mapping to map relations, is it right ?

and thanks again for your help and care
 
ntumba lobo
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well actually regarding the eager loading, this is a manual earger loading.
You would still have your hibernate mapping for your relationships but most of them are going to be configured as lazy.
And in your busines service you would explicitly call the getter of the collection that is required by your view.

As an example say you have a User object with 2 collections Groups and Friends.
You have a view1 that displays a User with his Friends but no need for the Group.
You have a view2 that displays a User with his Groups but no need for the Friends.

You would have lazy conf for both collections and 2 business methods say getUserWithGroups() and getUserWithFriends()
getUserWithGroups() would look like this
User user = session.loadById(User.class,123);
user.getGroups(); //this loads the groups only, but not the Friends as nothing will ask for it in the view1

getUserWithFriends() would look like this
User user = session.loadById(User.class,123);
user.getFriends(); //this loads the friends only, but not the Groups as nothing will ask for it in the view2

I hope that clarifies things

 
Amr k. Saleh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that sounds good for me, thanks a bunch ntumba for your help
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic