Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Difference between session.get and session.load

From the API, I could see it has something to do with proxy. But I couldn't find a lot of information on proxy and do not understand the difference between calling session.get and session.load. Could someone please explain or direct me to a reference page?

Thank you!!

like image 509
tomato Avatar asked Mar 04 '09 01:03

tomato


People also ask

Which is faster get or load?

The get() method fetches data as soon as it's executed while the load() method returns a proxy object and fetches only data when object properties is required. So that the load() method gets better performance because it support lazy loading.

What is Session load?

load() It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object.

What is Session get method in hibernate?

Hibernate Session provide different methods to fetch data from database. Two of them are – get() and load(). get() returns the object by fetching it from database or from hibernate cache. when we use get() to retrieve data that doesn't exists, it returns null, because it try to load the data as soon as it's called.

What is the difference between Session Save () and Session persist () method?

persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. whereas in save(), INSERT happens immediately, no matter if you are inside or outside of a transaction.


2 Answers

From the Hibernate forum:

This from the book Hibernate in Action. Good one read this..


Retrieving objects by identifier The following Hibernate code snippet retrieves a User object from the database:

User user = (User) session.get(User.class, userID); 

The get() method is special because the identifier uniquely identifies a single instance of a class. Hence it’s common for applications to use the identifier as a convenient handle to a persistent object. Retrieval by identifier can use the cache when retrieving an object, avoiding a database hit if the object is already cached. Hibernate also provides a load() method:

User user = (User) session.load(User.class, userID); 

The load() method is older; get() was added to Hibernate’s API due to user request. The difference is trivial:

If load() can’t find the object in the cache or database, an exception is thrown. The load() method never returns null. The get() method returns null if the object can’t be found.

The load() method may return a proxy instead of a real persistent instance. A proxy is a placeholder that triggers the loading of the real object when it’s accessed for the first time; On the other hand, get() never returns a proxy. Choosing between get() and load() is easy: If you’re certain the persistent object exists, and nonexistence would be considered exceptional, load() is a good option. If you aren’t certain there is a persistent instance with the given identifier, use get() and test the return value to see if it’s null. Using load() has a further implication: The application may retrieve a valid reference (a proxy) to a persistent instance without hitting the database to retrieve its persistent state. So load() might not throw an exception when it doesn’t find the persistent object in the cache or database; the exception would be thrown later, when the proxy is accessed. Of course, retrieving an object by identifier isn’t as flexible as using arbitrary queries.

like image 178
duffymo Avatar answered Sep 27 '22 19:09

duffymo


Well, in nhibernate at least, session.Get(id) will load the object from the database, while session.Load(id) only creates a proxy object to it without leaving your server. Works just like every other lazy-loaded property in your POCOs (or POJOs :). You can then use this proxy as a reference to the object itself to create relationships, etc.

Think of it like having an object that only keeps the Id and that will load the rest if you ever need it. If you're just passing it around to create relationships (like FKs), the id is all you'll ever need.

like image 45
Jorge Alves Avatar answered Sep 27 '22 19:09

Jorge Alves