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.