Hibernate get and load methods examples
In the hibernate there is 2 method to get the data from the database. They are get() and load() methods.
session.load()
It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, a proxy is an object with the given identifier value, its properties are not initialized yet, it just looks like a temporary fake object. If no row found, it will throw an ObjectNotFoundException.
session.get()
It always hit the database and returns the real object, an object that represents the database row, not a proxy. If no row found, it returns null.
get method example
Suppose we have
Employee and Address should have a "one-to-many" relationship when you want to save an Address, we have the code like that,
Employee emp = (Employee)session.get(Employee.class, new Integer(1));
Address addr = new Address();
//set stockTransactions detail
emp.setAddress(addr);
session.save(emp);
In session.get(), Hibernate will hit the database to retrieve the Employee object and put it as a reference to Address. However, this save process is extremely high demand, there may be thousand or million transactions per day. It results in poor performance.
session.load() example
Employee emp = (Employee)session.load(Employee.class, new Integer(1));
Address addr = new Address();
//set address
emp.setAddress(addr);
session.save(emp);
In case of a session.load(), Hibernate will not hit the database to retrieve the Employee object, it will return an Employee proxy object – a fake object with a given identify value.
In this scenario, a proxy object is sufficient to save an employee address value.
Exception in case of the session.get method
The get method returns null when no record found in the database. It will not throw any exception.
Employee emp = (Employee)session.get(Employee.class, new Integer(1));
System.out.println(emp.getName()); //java.lang.NullPointerException
It will always return null if the identity value is not found in the database.
Exception in case of the session.load method
The session.load method always throws org.hibernate.ObjectNotFoundException if there is no record found with the given identifier. It's hibernate specific Runtime Exception, so we don't need to catch it explicitly.
Employee emp = (Employee)session.load(Employee.class, new Integer(1)); //proxy
//initialize proxy, no row for empId 1 , throw ObjectNotFoundException
System.out.println(emp.getName());
In the following table the basic differences between get() and load() methods in the hibernate
Since. We should use get() when we want to make sure data exists in the database.
get method | load method |
---|---|
The get() loads the data as soon as it's called. | The load() returns a proxy object and loads data only when it’s actually required, so load() is better because it supports lazy loading |
The get method returns null when no record found in the database. | The load() throws an exception when data is not found, so we have to use it only when we know data exists |
The get method directly hit the database. | The load() method not hit the database directly. |
Situations where we have to use get() and load() method's
1). Use get() when you want to load an object.
2). Use load() when you need to obtain a reference to the object without issuing extra SQL queries, for example, to create a relationship with another object.
In this article, we have seen Hibernate get and load methods examples.
0 Comments
Post a Comment