Hibernate load vs get...confused on Hibernate Proxies too..
Joined: Mar 03, 2013
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.
1. By default, hibernate creates run-time proxies. It loads the objects as a proxy unless a fetch mode is specified or set to false.
2. load() always retrieves proxy objects. If called more than once within same session, it reads data from persistent context cache.
3. That's because once the object is loaded in cache, the next subsequent calls perform repeatable read.
4. Yes, you can read it. Although the state of this object changes from persistent to detached.
Tushar (SCJP 1.5)
Joined: Mar 03, 2013
Thanks for reply.
But still have following doubts
1)Hibernate default fetching strategy I used get method to retrieve a record from database and print the class name and this was output
Publisher class name: class com.sss.hib.Publisher (Normal Class)
I used load method to retrieve a record from database and print the class name and this was output
Publisher class name: class com.sss.hib.Publisher_$$_javassist_0 (Proxy Class)
Hibernate default fetch mode is eager right.
So from output,get method which uses eager fetching doesn't load proxy right,so here hibernate is not creating any default run time proxies right.Please correct me if iam wrong.
So the whole concept of run time proxies is,they get created if lazy loading is disabled right ( means lazy set to false). Please correct me if iam wrong.
2)Regarding Cache associated with session Even though session is closed ,application can still use cache associated with session.Closing session means further we don't have connection to database right.
So using get method to retrieve Employee object we can get name of employ even when session is closed.But when load method is used, outside session when we try to get employ
name we cant because there is no database connection .
3)Difference between get and load
If both get and load return data from cache after first call.Then whats difference between them except that load throws ObjectNotFoundException and get returns Null.
As a performance point of view i read in a document like if a object is being used more times in a transaction retrieve object using load method rather than get method as former retrieves data from cache and latter from database for every call to fetch record.
Can you please clear me on this
EAGER - means load all the associated items whenever the entity is loaded.
LAZY - loads only the proxy object.
The entity can be retrieved in 2 ways.
load() - returns the proxy object with an identifier.
get() - returns the complete object from database.
By default, hibernate loads all entities by lazy fetching strategy. It can be disabled by setting lazy = false on Entity definition.
In real time applications, entity is mostly associated with other items (mapped by colledtions)
By default, whenever an entity is loaded, only the proxy objects of the associated items are retrieved into the persistent context. This means, lazy = true.
but in case you want to load the associated objects - you can set fetch mode to EAGER to retrieve all associated objects of that entity. e.g.
1. covered above.
3. Correct. You've got the difference correct. Performance - Its upto you to optimize the fetching mode / startegy. Yes, get() hits the database everytime its invoked. but then there are some cases where you'd want eager load too.