Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes Hibernate load vs get, confused on Hibernate Proxies too Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate load vs get, confused on Hibernate Proxies too" Watch "Hibernate load vs get, confused on Hibernate Proxies too" New topic
Author

Hibernate load vs get, confused on Hibernate Proxies too

ShekarChandu Reddy
Greenhorn

Joined: Mar 03, 2013
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.
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 107

Hi Shekhar,

Welcome to the Ranch

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.


Thanks,
Tushar (SCJP 1.5)
ShekarChandu Reddy
Greenhorn

Joined: Mar 03, 2013
Posts: 2
Hello Mishra,
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 .
Hence LazyInitilizationException.

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

T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 107

fetch mode is used to load objects in association.
fetch strategy is used to load data from database for entities.

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.
2. correct
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.
Praveen Rachakulla
Greenhorn

Joined: Jun 05, 2013
Posts: 1
A quick question, lets say if there is no record in database with Id(100) for example, and if we use load() method then we will get the result as 100, which is not correct.
So if this is case then we will always end up in getting the records even though the records does not exist in database.
Can anyone please tell me in real world as where this will have an impact on get or load methods or in what scenarios we will be using get() method or load().
I am now aware of performance but would like to know what if in case of data retrievals kind of stuff I am looking.
I would really appreciate if anyone let me know the difference in terms of the Application perspective, I am bit interested in knowing the depth of the details.

Thanks,
Praveen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate load vs get, confused on Hibernate Proxies too
 
Similar Threads
spring hinbernet integration session problem
How to NOT fetch a collection?
EJB3 org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role
Hibernate Lazy Loading problem
What does a SessionFactory object cache?