4

Suppose there is a Parent class and I load it using lazy = "true".

Is there a way to verify that the Set is not loaded in the memory before an explicit call is made for it?

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

The child class:

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

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

Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92
  • are you using JPA2..?? – Ankur Singhal Aug 08 '16 at 06:06
  • @ankur-singhal No Hibernate 5 – Pritam Banerjee Aug 08 '16 at 06:07
  • @Pritam Banerjee one you call `parent.getChild()`, enable sql logging, and see it would be hitting your database to fetch agianst child id's – Ankur Singhal Aug 08 '16 at 06:09
  • @PritamBanerjee also if you have some doubt, better post some code like particular entity you are targeting at – Ankur Singhal Aug 08 '16 at 06:11
  • @ankur-singhal I have already enabled sql logging. That is not what I am looking for. I am trying to verify that this the child object is not loaded in memory. That is all. – Pritam Banerjee Aug 08 '16 at 06:15
  • 2
    Good question. Through the JPA API I think there is no chance, but some educated guesswork: using the Hibernate API method [AbstractPersistentCollection.wasInitialized ?](https://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/collection/internal/AbstractPersistentCollection.html#wasInitialized--) – Gimby Aug 08 '16 at 06:32
  • @Gimby Thanks for pointing me to right direction. I used `isInitialized()`. – Pritam Banerjee Aug 08 '16 at 06:56

1 Answers1

3

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 one returns false and the second one true.

Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92