In the previous post we have seen how to persist collection objects. Now after storing the details in the data
base, at some point of time we might need to fetch or retrieve these values. We have seen that hibernate creates
two tables. First table is the main table and the second table is for the collection objects. For fetching the
details we use the below code:
//line 1 userDetails = (UserDetails) session.get(UserDetails.class, 1); //line 2 userDetails.getListOfAddresses();
The first line in the above code is used to get all the details of the user which includes the collection objects.
But hibernate does not get all the details. Instead it fetches only the details in the UserDetails table i.e., it
fetches UserId and UserName. Whenever user requires list of addresses, user can add the line 2 code and get the values.
This concept is known as "lazy initialization". Lazy initialization means, you do not initialize the
entire object. You only initialize the first level variables member variables of the object and then you initialize
the list only you need the list or only when we access the list.
"Eager" is quite opposite to lazy initialization. Eager initializes all the variables irrespective
when they are accessed or when they are needed. In other words hibernate fetches all the data immediately. By default
hibernate uses lazyinitialization which improves performance. We can configure hibernate to fetch the details by
"Eager" by using the "fetch=FetchType.EAGER".
@ElementCollection(fetch=FetchType.EAGER)
How hibernate manages lazy initializing?
Hibernate creates an object called Proxy Object. An object proxy is just a way to avoid retrieving an object until
you need it.
I really like blogging introduced to get or about some thing I believe throughout. I additionally read reports blogs often and locate that this makes me feel far more intelligent every time I read them.
ReplyDeletewww.imarksweb.org
You’re extremely magnificent! You know what to put and how
ReplyDeleteto gather your readers. I appreciate you and for writing a well – written article, informational.
www.n8fan.net
Thanks goong :)
DeleteHi Barbie, the site does not open.
ReplyDeleteThanks Lee Woo
ReplyDeleteThank you Cindy :)
ReplyDelete