Is there a way to verify that the Lazy loaded object is not in a Hibernate session?

Suppose there is a parent class, and I load it with lazy = "true".

Is there a way to verify that a collection is not loaded into memory before an explicit call has been made to it?

public class Parent{
   private Intger parentId;
   Set <Child> child = new HashSet(); 
}

Class child:

public class Child{
   private Integer childId;
   Parent p; 
}

When I load the parent element before the call parent.getChild(), is there a way to verify that the child is not loaded into memory?

+4
source share
1 answer

To test the application, the following function was used:

Set<Child> childLazy = parentLazyLoaded.getChild();
Set<Child> childEager = parentEagerLoaded.getChild();

//then use the following methods
System.out.println("Lazy Loaded: " + Hibernate.isInitialized(childLazy));
System.out.println("Eager Loaded: " + Hibernate.isInitialized(childEager));

First comes back false, and the second true.

+3
source

Source: https://habr.com/ru/post/1650502/


All Articles