With Hibernate, we can use session.get() or session.load() to save an Object to database.
Using get() method
The following example reads an object of Employee class with id=1 from database.
package com.topjavatutorial; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column private String name; @Column private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "Employee{id = " + getId() + ", name = " + getName() + ", age = " + getAge() + "}"; } }
The following code shows how we can save an object to the database:
package com.topjavatutorial; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateDemo { public static void main(String[] args) { Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); SessionFactory factory = configuration.buildSessionFactory(); Session session = HibernateSessionUtil.getSession(); Employee employee = (Employee) session.get(Employee.class,1); if(employee != null) System.out.println(employee); session.close(); factory.close(); } }
Output :
Hibernate:
select
employee0_.id as id1_0_0_,
employee0_.age as age2_0_0_,
employee0_.name as name3_0_0_
from
Employee employee0_
where
employee0_.id=?
Employee{id = 1, name = John Doe, age = 21}
The session.get() method returns the Employee instance if it finds a match..otherwise, it returns null.
So, when using get() method to retrieve objects from database, it is better to have a null check before accessing the properties.
Employee employee = (Employee) session.load(Employee.class,2); if(employee != null) System.out.println(employee); else System.out.println("employee is null");
Output:
employee is null
Using load() method
load() method uses a proxy object while querying the database.
It returns a dummy object without hitting the database if a match is found in the session cache. If the object is not found in session cache, it hits the database.
Employee employee = (Employee) session.load(Employee.class,1); if(employee != null) System.out.println(employee); else System.out.println("employee is null");
Output:
Employee{id = 1, name = John Doe, age = 21}
If no match is found for the record in the database, then it returns an ObjectNotFoundException.
Employee employee = (Employee) session.load(Employee.class,2); if(employee != null) System.out.println(employee); else System.out.println("employee is null");
Output:
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.topjavatutorial.Employee#2]
at org.hibernate.internal.SessionFactoryImpl$1$1.handleEntityNotFound(SessionFactoryImpl.java:253)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:262)
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post