Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate lazy loading not working

I'm using version 3.6.1.Final

I have the following property in my entity bean

    @JoinColumn( name = "FOLDER_PARENT_ID", referencedColumnName = "FOLDER_ID" )
@ManyToOne(cascade=CascadeType.MERGE, fetch= FetchType.LAZY )
private FolderTbl parent;

In my unit test, Assertnull fails because getParent() is not null

assertNull( folderTbl.getParent() );

What else do I have to do to stop hibernate loading the parent?

like image 620
Farouk Alhassan Avatar asked Mar 03 '11 12:03

Farouk Alhassan


People also ask

How set lazy load in Hibernate?

To enable lazy loading explicitly you must use “fetch = FetchType. LAZY” on an association that you want to lazy load when you are using hibernate annotations. @OneToMany( mappedBy = "category", fetch = FetchType.

Is lazy loading enabled by default in Hibernate?

Lazy collection loads the child objects on demand, it is used to improve performance. Since Hibernate 3.0, lazy collection is enabled by default.

How do I enable lazy loading?

Lazy loading means delaying the loading of related data, until you specifically request for it. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook. Lazy loading is pretty much the default.

Is lazy loading working?

How to Check if Lazy Loading Is Working. If you're not sure if lazy loading is working correctly, you can open the Chrome DevTools and check that the images are not loaded until the scroll.


1 Answers

Even if you set the lazy to true, the parent value will not be null. The lazy load uses a proxy object and assign it to the parent property. When we try to use the parent(call getParent()) it will load the actual parent object using the proxy object.

If you do not want to load the object do not configure the JPA properties for the item and set it as transient.

like image 69
Arun P Johny Avatar answered Oct 07 '22 13:10

Arun P Johny