Proxy Objects and Eager and Lazy Fetch Types

Hibernate Proxy Object:  An object proxy is just a way to avoid retrieving an object until you need it.
In our example user class has the three field values-
1. User Id
2. User Name
3. List of the Addresses
If you want retrieve user object from the database, so what field value are retrieved from the database and which field are initialized. Suppose one user XYZ has the 100 addresses in the database and we want to retrieved the name of this user from database. For that we retrieve the user object, now question is this what about the field listOfAddress field, Is it also have the value? if you say yes so what about cost of memory ? if you say no so how to retrieve the value associated with that field the address table in the database on demand.

-A
 |
 *---B
 |   |
 |   *---C
 |   |
 |   *---D
 |       |
 |       *---E
 |       |
 |       *---F
 |       |
 |       *---G
 *---H

Ok lets see in the given below flow of the diagram.

In  Hibernate 2 does not proxy objects by default. However, experience has shown that using object proxies is preferred, so this is the default in Hibernate 3.

In Hibernate 3, when we fetching the user object from the database actually its retrieved the proxy object of the user class means only first level of the fields are initializing with the associated values from the database. Field listOfAddresses does not have the value. If you want the list address you should call the following method you will get the listOfAddresses.
                                        user.getListOfAddress(); —>> this return the list of the address associated with that particular user which name is XYZ this is the default behavior of the Hibernate 3

Now we look about the fetching strategies.
Fetching Strategies:  there are two types of the fetching strategies in the hibernate.
1. Lazy Fetch type
2. Eager Fetch type
 LAZY = fetch when needed EAGER = fetch immediately
1. Lazy Fetch Type: This the default fetch type of the hibernate 3.
Now when you load a User from the database, JPA loads its id, name, and address fields for you. But you have two options for users: to load it together with the rest of the fields (i.e. eagerly) or to load it on-demand (i.e. lazily) when you call the user’s getListOfAddresses() method.

Lazy/Select Fetch strategy:- Select Fetch strategy is the lazy fetching of associations. The purpose of Lazy strategy is memory optimization . When I say memory optimization it means it means it saves us from heap error. This is what I think. So we can say yes if we are loading too objects in aseesion we should go for Lazy Fetch strategy but in terms of time performance it does not provide any Benefit. Agreed?

When a user has many addresses it is not efficient to load all of its addresses with it when they are not needed. So in suchlike cases, you can declare that you want addresses to be loaded when they are actually needed. This is called lazy loading.

2. Eager Fetch Strategy: In hibernate 2 this is default behavior of the to retrieving an object from the database.
Eager/Join Fetch strategy:- Join Fetch strategy the eager fetching of associations.The purpose of Join Fetch strategy is optimization in terms of time.I mean even associations are fetched right at the time of fetching parent object. So in this case we don’t make database call again and again . So this will be much faster.Agreed that this will bad if we are fetching too many objects in a session because we can get java heap error.
So now can we say in the hibernate session where we are not loading too many objects we should go for Eager fetch as it will be much better in terms of time response(Any ways memory will be reclaimed by garbage collector once we close the session).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s