Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Lazy Loading in Hibernate

How do I disable lazy loading in Hibernate? I am using persistence annotations, not an hbm xml file.

I am fetching a single object by ID and want all properties loaded. The session is closed before I use the object.

Thanks!

like image 373
Jonah Avatar asked Mar 29 '11 21:03

Jonah


1 Answers

You need to annotate the properties that you want non-lazy loaded with FetchType.EAGER

   @ManyToOne(fetch = FetchType.EAGER)

You see, it isn't the object that you are loading that is lazy loaded. Rather, that object's associations are lazy, and you need to tell them not to be if that is your desired behavior.

If those objects also have associations that you want loaded eagerly, you need to annotate them as well.

like image 166
Sean Adkinson Avatar answered Sep 17 '22 18:09

Sean Adkinson