Here UserDetails is the parent and Address is the child. Sleep mode is actually lazy - it loads the child's Address . Therefore, in the end, all child elements ( Address in this case) are not preloaded when you load the parent ( UserDetails in this case).
So when you do this:
user = (UserDetails) session.get(UserDetails.class, 1);
Hibernation does not actually load all child elements ( Collection<Address> ). Instead of Hibernate, load Address only with direct access to them. This way hibernate will not use DB for the Address table unless you really need them and that is the purpose of lazy loading .
What Lazy loading means is that as long as you get the UserDetails proxy object, it really doesn't get into the Address table unless you try to access the elements of the collection explicitly. In other words, you need to iterate over the sleep collection to get the Address table.
You may find yourself in a situation where each time you get into the database for each child ( Address ). So make an explicit call to listOfAddresses.size() to load all the children at a time.
Also note that Lazy loading will occur by default for One-to-Many and Many-to-Many cases.
source share