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?
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.
Lazy collection loads the child objects on demand, it is used to improve performance. Since Hibernate 3.0, lazy collection is enabled by default.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With