get vs load in Hibernate
Difference between get and load method in Hibernate is one of the most popular questions asked in Hibernate and spring interviews. Hibernate Session class provides two methods to access object e.g. session.get() and session.load() both looked quite similar to each other but there are subtle differences between load and get method which can affect the performance of the application. The main difference between get() vs load method is that get() involves database hit if an object doesn't exist in Session Cache and returns a fully initialized object which may involve several database calls while load method can return proxy in place and only initialize the object or hit the database if any method other than getId() is called on persistent or entity object.
This lazy initialization can save a couple of database round-trip which results in better performance.
By the way, there are many articles on interview questions in Java, you can use the search button on the top left to find them. Some of them like 20 design pattern interview questions and 10 Singleton pattern questions are my favorites, you may also like.
Coming back to the article, you can find more differences between load and get in the rest of this article in point format but this is the one which really makes difference while comparing both of them. If you look at how to get and to load get called it's pretty identical.
And, if you are serious about improving your Hibernate and JPA skills then I also recommend you check out Master Hibernate and JPA with Spring Boot in 100 Steps course by Ranga Rao Karnam on Udemy. It's a great course to learn Hibernate and JPA with Spring Boot in a hands-on and practical manner. It's also very affordable and you can buy in just $10 on Udemy flash sales.
This lazy initialization can save a couple of database round-trip which results in better performance.
By the way, there are many articles on interview questions in Java, you can use the search button on the top left to find them. Some of them like 20 design pattern interview questions and 10 Singleton pattern questions are my favorites, you may also like.
Coming back to the article, you can find more differences between load and get in the rest of this article in point format but this is the one which really makes difference while comparing both of them. If you look at how to get and to load get called it's pretty identical.
And, if you are serious about improving your Hibernate and JPA skills then I also recommend you check out Master Hibernate and JPA with Spring Boot in 100 Steps course by Ranga Rao Karnam on Udemy. It's a great course to learn Hibernate and JPA with Spring Boot in a hands-on and practical manner. It's also very affordable and you can buy in just $10 on Udemy flash sales.
Difference between get and load method in Hibernate
Here are a few differences between the get and load method in Hibernate.
1. Behavior when Object is not found in Session Cache
Apart from performance, this is another difference between get and load which is worth remembering. get method of Hibernate Session class returns null if an object is not found in the cache as well as on database while load() method throws ObjectNotFoundException if the object is not found on the cache as well as on database but never return null.
2. Database hit
Get method always hit the database while load() method may not always hit the database, depending upon which method is called.
3. Proxy
Get method never returns a proxy, it either returns null or fully initialized Object, while load() method may return proxy, which is the object with ID but without initializing other properties, which is lazily initialized. If you are just using the returned object for creating a relationship and only need Id then load() is the way to go.
4. Performance
By far the most important difference between get and load in my opinion. get method will return a completely initialized object if Object is not on the cache but exists on Database, which may involve multiple round-trips to the database based upon object-relational mappings while load() method of Hibernate can return a proxy which can be initialized on demand (lazy initialization) when a non-identifier method is accessed.
Due to the above reason use of the load method will result in slightly better performance, but there is a caveat that proxy objects will throw ObjectNotFoundException later if the corresponding row doesn’t exist in the database, instead of failing immediately so not a fail fast behavior.
Due to the above reason use of the load method will result in slightly better performance, but there is a caveat that proxy objects will throw ObjectNotFoundException later if the corresponding row doesn’t exist in the database, instead of failing immediately so not a fail fast behavior.
5. Availability
The load method exists prior to get() method that is added on user request.
Here is a nice diagram which effectively explains the real difference between getting and load in Hibernate
When to use Session get() and load() in Hibernate
So far we have discussed how get and load are different to each other and how they can affect the performance of your web application, after having this information in our kitty we can see some best practices to get most of the load and get together. This section suggests some scenario which helps you when to use get and load in Hibernate.
1. Use get method to determine if an instance exists or not because it can return null if the instance doesn’t exist in cache and database and use load method to retrieve instance only if you think that instance should exist and non-availability is an error condition.
2. As stated in the difference number 2 between get and load in Hibernate. get() method could suffer a performance penalty if only identifier method like getId() is accessed. So consider using load method if your code doesn't access any method other than identifier or you are OK with lazy initialization of object if the persistent object is not in Session Cache because load() can return proxy.
How to call get records in Hibernate using get and load method
If you look at the below code, there is not much difference in calling get() and load() method, though both are overloaded now and can accept few more parameters the primary methods looks exactly identical. It’s their behavior which makes them different.
//Example of calling get method of Hibernate Session class
Session session = SessionFactory.getCurrentSession();
Employee Employee = (Employee) session.get(Employee.class, EmployeeID);
//Example of calling load method of Hibernate Session
Session session = SessionFactory.getCurrentSession();
Employee Employee = (Employee) session.load(Employee.class, EmployeeID);
Session session = SessionFactory.getCurrentSession();
Employee Employee = (Employee) session.get(Employee.class, EmployeeID);
//Example of calling load method of Hibernate Session
Session session = SessionFactory.getCurrentSession();
Employee Employee = (Employee) session.load(Employee.class, EmployeeID);
That’s all on the difference between get and load in Hibernate. No doubt Hibernate is a great tool for Object-relational mapping but knowing these subtle differences can greatly help to improve the performance of your J2EE application, apart from practical reason get vs load method is also frequently asked questions in Hibernate interview, so familiarity with differences between load and get certainly helps.
Other Hibernate Articles and Interview Questions you may like
- Difference between First and Second level cache in Hibernate? (answer)
- Difference between get() and load() method in Hibernate? (answer)
- 5 Spring and Hibernate Training Courses for Java developers (list)
- 2 Books to Learn Hibernate in Depth (books)
- 5 Books to Learn Spring Framework in Depth (books)
- Why Hibernate Entity class should not be final in Java? (answer)
- 10 Hibernate Questions from Java Interviews (list)
- Top 5 Courses to learn Hibernate and JPA in depth (courses)
- Top 5 Courses to learn Spring Data JPA in depth (spring data courses)
- Difference between Spring Boot and Spring Cloud (answer)
- Difference between save and persist in hibernate (answer)
Thanks for reading this article, if you like this article and interview question then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.
4 comments :
Actually , get() does not always hit database , and it can return a proxy .
Like what you said , get() will only hit DB if the object doesn't exists in Session Cache. Hibernate will first check if the same object , no matter it is the real object or proxy , exist in the session cache first . If yes , just return that object .If not , hit database to get it . So after a new session starts , if you first use load() to get a object , a proxy will be initialized and put in that session cache , then you use get() in the same session to get the same object , that proxy in the session cache will be returned instead of hitting DB and get this object
@Above
In that case if i get proxy object from session when i use get() and if i call get properties of my object will it hit DB? when we use load proxy object will hit DB when we call getter properties of my obj.
<>
check above line in option 4. Performance.
I think it should be instead of failing immediately so not a "fail safe" behavior
OR I think it should be instead of failing immediately so its a "fail fast" behavior.
Tell me if I'm wrong..
Datta
Is this sentence correct "if any method other than getId() is called on persistent or entity object",
As i believe it will hit database if we try to access any of its object??
Please correct me if i am wrong.
Post a Comment