Learn the difference between get() vs load() methods to fetch entity by id from the database using Hibernate.
In hibernate, load() always return a “proxy” object without hitting the database and get() always return the real object after fetching from the database,
1. Using Session.load() API
Hibernate’s Session
interface provides several overloaded load()
methods for loading entities from the database. Each load()
method requires the object’s primary key as an identifier, and it is mandatory to provide it.
In addition to the ID, hibernate also needs to know which class or entity name to use to find the object with that ID. After the load()
method returns, we need to cast the returned object to a suitable type of class to further use it. It’s all that load()
method needs from us to work correctly.
Let’s look at different flavors of load()
method available in hibernate Session interface.
public Object load(Class clazz, Serializable id) throws HibernateException public Object load(String entityName, Serializable id) throws HibernateException public void load(Object object, Serializable id) throws HibernateException
- The first method needs the Class type which we would like to load along with the primary key.
- The second method asks for entityName and the primary id. Both methods return the populated entity object as a return value which we will cast to the desired type.
- The third takes an object as an argument. The object should be of the same class as the object we would like loaded, and it should be empty. Hibernate will populate that object with the information we requested.
The other load()
methods available through hibernate session take a lock mode as an argument too. The lock mode specifies whether Hibernate should look into the cache for the object and which database lock level Hibernate should use for the row (or rows) of data that represent this object.
In the official documentation, hibernate developers claim that Hibernate will usually pick the correct lock mode for us, although in some situations it is important to manually choose the correct lock.
Let’s look at the examples of each loading method in the simplest form to be clear about what we read above.
Long id = 1001; //An employee's primary id
//1
EmployeeEntity entity = (EmployeeEntity)
session.load(EmployeeEntity.class, id);
//2
EmployeeEntity entity = (EmployeeEntity)
session.load("com.howtodoinjava.demo.entity.EmployeeEntity", id);
//3
EmployeeEntity entity = new EmployeeEntity();
session.load(entity , id);
2. Using Session.get() API
The get()
method is very much similar to load()
method. The get()
methods take an identifier, and either an entity name or a class types.
There are also two more get()
methods that take a lock mode as an argument.
public Object get(Class clazz, Serializable id) throws HibernateException
public Object get(String entityName, Serializable id) throws HibernateException
There is not much difference in code while working with either load()
or get()
method, all you need is to replace the load()
method with get()
method in the first two examples. There is no get()
equivalent to last load()
method.
Long id = 1001; //An employee's primary id
//1
EmployeeEntity entity = (EmployeeEntity)
session.get(EmployeeEntity.class, id);
//2
EmployeeEntity entity = (EmployeeEntity)
session.get("com.howtodoinjava.demo.entity.EmployeeEntity", id);
3. Difference between load() and get() Methods
Why do we have two methods to do the same job. Actually, this is a frequently asked interview question as well.
The difference between get() and load() methods lies in the return value when the identifier does not exist in the database.
- The entity loaded with get() method is eager loaded and contains information from the database. Whereas the entity loaded from load() method is a lazy load proxy object that queries the database when any field information is accessed.
- In case of
get()
method, we will get the return value as NULL if the identifier is absent. But in the case ofload()
method, we will get ObjectNotFoundException.
The exception in the case of the load() method will look like this:
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
[com.howtodoinjava.demo.entity.EmployeeEntity#23]
at org.hibernate.internal.SessionFactoryImpl$1$1.handleEntityNotFound(SessionFactoryImpl.java:253)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
at org.hibernate.internal.SessionImpl.load(SessionImpl.java:940
4. Conclusion
In this hibernate tutorial, we learned the basics of load() and get() methods to fetch entity information from the database. We also learned the differences between both methods.
Happy Learning !!