Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
In Hibernate, both get()
and load()
methods are used to retrieve an object from the database, but they differ in the way they operate. Let's dive into a tutorial to understand the differences between get()
and load()
methods:
get()
MethodThe get()
method belongs to the Session
class and is used to fetch an entity based on its primary key. If the entity isn't found, it will return null
.
Syntax:
public Object get(Class theClass, Serializable id) throws HibernateException;
Usage:
Session session = sessionFactory.openSession(); Student student = (Student) session.get(Student.class, studentId);
Characteristics:
null
if the object is not found.load()
MethodThe load()
method also belongs to the Session
class and retrieves an entity based on its primary key. However, it will throw an ObjectNotFoundException
if the entity isn't found. It's designed to work with proxies and might not hit the database immediately.
Syntax:
public Object load(Class theClass, Serializable id) throws HibernateException;
Usage:
Session session = sessionFactory.openSession(); Student student = (Student) session.load(Student.class, studentId);
Characteristics:
ObjectNotFoundException
if the object is not found.load()
is more efficient.Return Value:
get()
: Returns null
if object is not found.load()
: Throws an ObjectNotFoundException
if object is not found.Database Hit:
get()
: Hits the database immediately.load()
: Returns a proxy by default and hits the database when the first method is accessed on the proxy object.Performance:
get()
: Less efficient if you are sure the entity exists and you'll access it later due to immediate database hit.load()
: More efficient in such cases because it leverages lazy loading.Let's assume you have a Student entity, and you're looking to retrieve a student with ID 12345
.
get()
and the student doesn't exist, it will return null
, and you'll know immediately.load()
, Hibernate might just return a proxy object without hitting the database. If you try to access a property on this student, and it turns out it doesn't exist, only then you'll get an ObjectNotFoundException
.In conclusion, you'd typically use get()
when you're not sure if an entity exists in the database and want a quick answer. On the other hand, if you're certain an entity exists and want to optimize performance using lazy loading, you'd use load()
.
Hibernate get()
vs load()
example:
get()
and load()
are used to retrieve entities by their primary key. The key difference lies in when the actual database query is executed. get()
fetches the entity immediately, while load()
fetches it lazily, meaning the actual database query is deferred until the entity is accessed.Session session = sessionFactory.openSession(); // Using get() Employee employeeGet = session.get(Employee.class, 1L); // Using load() Employee employeeLoad = session.load(Employee.class, 1L); // Accessing properties to trigger load System.out.println("Employee Name (get()): " + employeeGet.getName()); System.out.println("Employee Name (load()): " + employeeLoad.getName()); session.close();
Lazy loading with Hibernate load()
method:
load()
method in Hibernate allows lazy loading, meaning the actual database query is deferred until the accessed properties are needed. This is beneficial for performance if not all properties are immediately required.Session session = sessionFactory.openSession(); Employee employee = session.load(Employee.class, 1L); // No database query is executed at this point // Accessing properties triggers the database query System.out.println("Employee Name: " + employee.getName()); session.close();
Using get()
and load()
for entity retrieval in Hibernate:
get()
and load()
methods are used for retrieving entities by their primary key. get()
fetches the entity immediately, while load()
fetches it lazily.Session session = sessionFactory.openSession(); // Using get() Employee employeeGet = session.get(Employee.class, 1L); // Using load() Employee employeeLoad = session.load(Employee.class, 1L); session.close();