Apr
12
2015

proxy object and Lazy and Eager initialization in hibernate

In this tutorial i will explain what proxy object ,how hibernate support proxy object,what is lazy and eager fetch strategy that hibernate support.

First of all have a look in below scenario .Let suppose we each university have many collages associated with it.Now while we fetch a particular university data then do all the associated collages get fetch. Let us suppose  RTU university have more than 100 collages in under it.When we are fetching RTU university data from university table do all the associated collages get fetched.

If all the collage record getting pulled corresponding to university while i am interested only university object then it is useless.

Proxy object ,lazy and eager initialization

 

Let see complete program for this.

Collage.java

University.java

hibernate.cfg.xml

ProxyObject.java

Output : 2

From above output we see that hibernate pull second level object(collage records) also when we are actually fetching only university object. But what is happening behind the scene is different. Hibernate actually  does not pull any of second level object. It is default behavior of hibernate.

If you remove comment from line number 36 then you will get exception

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.freakers.proxy.University.collageList, could not initialize proxy – no Session

What is actually happening is that hibernate actually fetching second level object when we are accessing second level objects. In the following line hibernate only fetch first level object (i.e university object).

University university2 = (University)session.get(University.class, 1);

But when we are accessing second level object then hibernate make another query to fetch second level variable. The following line will ask hibernate to fetch second level object.

System.out.println(university2.getCollageList().size());

But as the session has been closed so hibernate can not make query for second level variable ,So it is throwing exception.

Proxy object in hibernate


When we are asking hibernate to fetch University object in following line hibernate does not actually giving us University object. It actually create proxy object of University . The proxy object only contain first level data only.

University university2 = (University)session.get(University.class, 1);

When we access second level variable then hibernate execute another query to fetch second level object and return proxy object that contain both first level and second level object.proxy object and lazy and eager initialization

When we fetch data then instead of giving actual object,  hibernate give us proxy object. When we access second level variable then hibernate make another query to fetch second level variable from DB.

Lazy and Eager initialization of object 


 

As we have seen above that hibernate fetch only first level variable when we fetch records. This behavior of hibernate is known as Lazy initialization. Initialization of first level variable is known as lazy initialization.

Well we can also configure hibernate to fetch next level record in this following statement

University university2 = (University)session.get(University.class, 1);

When hibernate fetches second level   record along with first level record then this behavior of hibernate is known as Eager initialization.

To fetch second level record along with first level records we need to configure hibernate in @ElementCollection annotation. By default hibernate uses Lazy fetch type.

@ElementCollection(fetch=FetchType.EAGER)

After adding fetch type in our University class

University.java

ProxyObject.java

OutPut: 2

Now there is no exception even after session is close because we have used Eager fetch strategy. So hibernate fetches collages associated with university at first query .and initialize second level variable.

This is all in this tutorial. Hope you enjoyed it. In next tutorial i will explain One to One mapping in hibernate.

 

Comments are closed.