-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate load vs get...confused on Hibernate Proxies too..
PostPosted: Sun Mar 03, 2013 8:30 am 
Newbie

Joined: Sun Mar 03, 2013 8:24 am
Posts: 2
Hello All

I am newbee to hibernate and wrote a sample application to find out difference between load and get.

What i analyzed was
load method uses lazy fetching and load method returns proxies
get method uses eager fetching and get method doesn't return proxies

1)Would like to know whether hibernate creates proxies only during lazy initialization...or for every scenario when a record is fetched from database hibernate creates proxies irrespective of type of fetching strategy?
I Mean when does hibernate uses/creates proxies?

And one other difference was with in same session

2) I used load method to fetch same record assuming that first time when load method is called it loads data from database,put data in an object and set it in cache associated with session,when i called load method for second time for same record,it should fetch from cache thus avoiding a database hit .So only one select sql even though we called load method twice. It is perfect

3) But when i used get method with in same session twice to fetch same record,as per get method API,it has to fetch data directly from database without checking cache even though same object was loaded earlier into cache,which means there should be 2 sqls.But with in same session,when i call get method for second time it didn't hit database directly instead it fetched data from cache.So i couldn't get exact underlying point ,can some one give me a clear understanding of this?

4)And one more thing when a session is closed can we able to get object from cache?Because in case of get method,say for example i fetched an Employee record from database.I closed session.After closing session still i can get name of Employee.So is it possible to do so?What i understood was for every record retrieved from database hibernate creates object and store object in cache associated with session.So does object exist as long as session is avaialble or even after session is closed as in case of get method.


Top
 Profile  
 
 Post subject: Re: Hibernate load vs get...confused on Hibernate Proxies too..
PostPosted: Mon Mar 04, 2013 7:27 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
load method uses lazy fetching and load method returns proxies
get method uses eager fetching and get method doesn't return proxies


Wether hibernate returns a proxy or not, does not depend in first line on the api you are using.
As first step hibernate checks if concerning entity is already cached in (= attached to) your persistent context (aka 1Level-Cache or transactional cache).
-If yes, then the retrieved cache entry is returned indipendently if it is a proxy or not.
-If not, then hibernate returns a proxy if you used the load method.

Quote:
1)Would like to know whether hibernate creates proxies only during lazy initialization...or for every scenario when a record is fetched from database hibernate creates proxies irrespective of type of fetching strategy?
I Mean when does hibernate uses/creates proxies?


There are 2 scenarios in which hibernate creates proxies:
-lazy initialization of relations when associated entity object is not already cached in persistent context.
-first access of a particular entity object in transaction through load method.

Quote:
3) But when i used get method with in same session twice to fetch same record,as per get method API,it has to fetch data directly from database without checking cache even though same object was loaded earlier into cache,which means there should be 2 sqls.But with in same session,when i call get method for second time it didn't hit database directly instead it fetched data from cache.So i couldn't get exact underlying point ,can some one give me a clear understanding of this?


This your presumption is simply wrong.
Once hibernate verifies that concerning entity object is already attached to your persistent context, it (get-method) will not hit the database anymore.


Quote:
4)And one more thing when a session is closed can we able to get object from cache?Because in case of get method,say for example i fetched an Employee record from database.I closed session.After closing session still i can get name of Employee.So is it possible to do so?What i understood was for every record retrieved from database hibernate creates object and store object in cache associated with session.So does object exist as long as session is avaialble or even after session is closed as in case of get method.


What cache are you meaning exactly?
I assume you mean the persistent context = the first-level-cache,OK?
And I also assume you mean the hiberante-session and not the database session,OK.
Under this assumtion (closing the session means also closing the persistent context) the entity objects continue to exist as long you reference them and the garbage collector don't have removed them yet: they are called detached objects and have now transient behavior. That means you still can read and write these objects, but changes are no more tracked by hiberante and any changes will no be written to the database anymore.


Top
 Profile  
 
 Post subject: Re: Hibernate load vs get...confused on Hibernate Proxies too..
PostPosted: Mon Mar 04, 2013 4:07 pm 
Newbie

Joined: Sun Mar 03, 2013 8:24 am
Posts: 2
Hello pb00067

Thanks for detailed explanation.

This is my understanding....after going thru ur explanation.Please correct me if iam wrong

If we query for any record,hibernate first it checks whether that record is available in persistence context or not irrespective of type of fetching strategy(lazy or eager).If it is available it will return the object which can be normal object or proxy object based on type of fetching strategy.

If a record is not found in persistence context then hibernate will hit database ,create object with data and store it in persistence context.Then if it is eager fetching it will return object as it is.Else if it is lazy fetching which can be for example load method or can be any class which has some child objects to be loaded like Employee class will have set of Addresses.Then hibernate will create a Proxy object and contains values for all field values of Parent class except field value of child class unless child class is called via getter method.

And scenario when hibernate creates runtime proxies is
Either all methods in hibernate which are declared to lazily load object for example load method or
For all Associations with in a class (for example Addresses of an Employee) which are to be lazily loaded

And (closing the session means also closing the persistent context) .Does it mean that ,when session is closed ,hibernate will not treat that cache memory as its own and that memory becomes part of JVM memory where all objects reside and gets eligible for garbage collector if the objects have no reference for some time.


Top
 Profile  
 
 Post subject: Re: Hibernate load vs get...confused on Hibernate Proxies too..
PostPosted: Mon Mar 11, 2013 10:14 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
If we query for any record,hibernate first it checks whether that record is available in persistence context or not irrespective of type of fetching strategy(lazy or eager).If it is available it will return the object which can be normal object or proxy object based on type of fetching strategy.


Correct, if you replace the last 6 words "based on type of fetching strategy" with
"depending how the object was loaded into persistent context on the first access".

Quote:
If a record is not found in persistence context then hibernate will hit database ,create object with data and store it in persistence context.Then if it is eager fetching it will return object as it is.Else if it is lazy fetching which can be for example load method or can be any class which has some child objects to be loaded like Employee class will have set of Addresses.Then hibernate will create a Proxy object and contains values for all field values of Parent class except field value of child class unless child class is called via getter method.


All correct except first sentence.
The database is only hit if it's eager fetching. In case of proxy creation there's no need to hit the database an extra time.


Quote:
And scenario when hibernate creates runtime proxies is
Either all methods in hibernate which are declared to lazily load object for example load method or
For all Associations with in a class (for example Addresses of an Employee) which are to be lazily loaded


Correct if you begin to work with an empty persisten context.
If you indeed have already read eagarly some object which are then targeted again by a lazy load,
then hibernate will not create proxies for them anymore.

Quote:
And (closing the session means also closing the persistent context) .Does it mean that ,when session is closed ,hibernate will not treat that cache memory as its own and that memory becomes part of JVM memory where all objects reside and gets eligible for garbage collector if the objects have no reference for some time.


Correct.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.