What is the Difference between session.get() and session.load() in Hibernate ? This is one of the most asked interview question in Hibernate. Both the get() and load() methods are defined in the Session interface and used to retrieve the information of a specific record. In this article, we will see what is the difference between then and when to use which method.
Creating table
Create an EMPLOYEE Table, simply Copy and Paste the following SQL query in the query editor to get the table created.
CREATE TABLE "EMPLOYEE" ( "ID" NUMBER(10,0) NOT NULL ENABLE, "NAME" VARCHAR2(255 CHAR), "AGE" NUMBER(10,0), "DEPT" VARCHAR2(255 CHAR), PRIMARY KEY ("ID") );
Folder Structure:
- Create a simple Maven Project “HibernateTutorial” and create a package for our source files “com.javainterviewpoint” under src/main/java
- Now add the following dependency in the POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>HibernateTutorial</groupId> <artifactId>HibernateTutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <hibernate.version>4.3.11.Final</hibernate.version> <oracle.connector.version>11.2.0</oracle.connector.version> </properties> <dependencies> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!-- Oracle --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>${oracle.connector.version}</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
- Create the Java classes Employee.java and GetVsLoad.java under com.javainterviewpoint folder.
- Place the hibernate.cfg.xml under the src/main/resources directory
Difference between session.get() and session.load() in Hibernate
Employee.java
Create a new Java file Employee.java under the package com.javainterviewpoint and add the following code
package com.javainterviewpoint; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="EMPLOYEE") public class Employee implements Serializable { private static final long serialVersionUID = -1280037900360314186L; private int id; private String name; private int age; private String dept; public Employee() { super(); } public Employee(int id, String name, int age, String dept) { super(); this.id = id; this.name = name; this.age = age; this.dept = dept; } @Id @GeneratedValue(strategy = GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name="NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="AGE") public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Column(name="DEPT") public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", dept=" + dept + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((dept == null) ? 0 : dept.hashCode()); result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (age != other.age) return false; if (dept == null) { if (other.dept != null) return false; } else if (!dept.equals(other.dept)) return false; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
Our Employee class is a simple POJO class consisting of the getters and setters for the Employee class properties (id, name, age, dept).
In the POJO class, we have used the below JPA Annotations.
- @Entity – This annotation will mark our Employee class as an Entity Bean.
- @Table – @Table annotation will map our class to the corresponding database table. You can also specify other attributes such as indexes, catalog, schema, uniqueConstraints. The @Table annotation is an optional annotation if this annotation is not provided then the class name will be used as the table name.
- @Id – The @Id annotation marks the particular field as the primary key of the Entity. We have specified the strategy as “AUTO” so that hibernate choose the optimal strategy for the database. Say for example SEQUENCE Strategy is supported by Oracle and not MySql and IDENTITY strategy is supported by MySQL but not Oracle. So it is better to leave it as AUTO.
- @Column – This annotation maps the corresponding fields to their respective columns in the database table.
hibernate.cfg.xml
Place the hibernate.cfg.xml file also under the src/main/resources folder
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@mydb:40051:dev</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Entity class --> <mapping class="com.javainterviewpoint.Employee" /> </session-factory> </hibernate-configuration>
- First and foremost property is for specifying the JDBC Driver class, in my case it OracleDriver
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- Give the connection URL for connecting the database and provide username and password for connecting the above database
<property name="hibernate.connection.url">jdbc:oracle:thin:@mydb:40051:dev</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property>
- Specify the connection pool size, this property limits the number of connections in the Hibernate connection pool.
<property name="connection.pool_size">1</property>
- Dialect Property makes the Hibernate generate the SQL for the corresponding database which is being used. In this example we are using Oracle database hence Oracle query will be generated. If you are using MySQL database then you need to change the dialect accordingly.
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
- The show_sql property will print the executed sql in the console when set to true.
<property name="show_sql">true</property>
- If the property “hibernate.hbm2ddl.auto” is set to “create” This will drop and recreate the database schema on every execution. If it is set to “update” then the database schema will be updated every time rather than dropping and recreating.
<property name="hibernate.hbm2ddl.auto">update</property>
- Under the Mapping tag class property give our Entity class name
<mapping class="com.javainterviewpoint.Employee" />
Consider we have an EMPLOYEE table which is created and already has the below record in it.
session.load()
session.load() will always return a “proxy” object without hitting the database. Proxy is nothing but a fake object created by hibernate with the given identifier value and the remaining values will not be even initialized. If no row is found then this method will throw “ObjectNotFoundException”
package com.javainterviewpoint; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class GetVsLoad { public static void main(String args[]) { //Reading the hibernate configuration file Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder(); srb.applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry = srb.build(); //Create SessionFacctory SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); //Create Session from SessionFactory Session session = sessionFactory.openSession(); //Begin the transaction session.beginTransaction(); //Create Employee object Employee employee = (Employee) session.load(Employee.class,new Integer(413)); System.out.println("*** Employee Details ***"); System.out.println("Employee Id : "+employee.getId()); System.out.println("Employee Name : "+employee.getName()); System.out.println("Employee Age : "+employee.getAge()); System.out.println("Department : "+employee.getDept()); //Commit the changes session.getTransaction().commit(); //Close the session session.close(); } }
Output
INFO: HHH000261: Table found: EMPLOYEE
Apr 13, 2017 5:12:20 PM org.hibernate.tool.hbm2ddl.TableMetadata
INFO: HHH000037: Columns: [id, age, name, dept]
Apr 13, 2017 5:12:20 PM org.hibernate.tool.hbm2ddl.TableMetadata
INFO: HHH000108: Foreign keys: []
Apr 13, 2017 5:12:20 PM org.hibernate.tool.hbm2ddl.TableMetadata
INFO: HHH000126: Indexes: [sys_c0015506]
Apr 13, 2017 5:12:20 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
*** Employee Details ***
Employee Id : 413
Hibernate: select employee0_.id as id1_0_0_, employee0_.AGE as AGE2_0_0_, employee0_.DEPT as DEPT3_0_0_, employee0_.NAME as NAME4_0_0_ from EMPLOYEE employee0_ where employee0_.id=?
Employee Name : JIP
Employee Age : 100
Department : IT
- In the above code, we have used session.load() to retrieve the employee with the ID 413, Hibernate immediately created the fake Employee object with the Id 413 and the remaining properties (Name, Age, Department) would not have been initialized.
- It will hit the database only when it tries to retrieve other properties of Employee object (ie) employee.getName() or employee.getAge() or employee.getDept().
- This is the reason, we did not see the below SQL query when we retrieved employee id (employee.getId()).
Hibernate: select employee0_.id as id1_0_0_, employee0_.AGE as AGE2_0_0_, employee0_.DEPT as DEPT3_0_0_, employee0_.NAME as NAME4_0_0_ from EMPLOYEE employee0_ where employee0_.id=?
- When the object with the id 413 is not found in the database, it will throw “ObjectNotFoundException”
session.get()
session.get() will always hits the database immediately and returns the original object. If no row is found then this method will return “null”.
package com.javainterviewpoint; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class GetVsLoad { public static void main(String args[]) { //Reading the hibernate configuration file Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder(); srb.applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry = srb.build(); //Create SessionFacctory SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); //Create Session from SessionFactory Session session = sessionFactory.openSession(); //Begin the transaction session.beginTransaction(); //Create Employee object Employee employee = (Employee) session.get(Employee.class,new Integer(413)); System.out.println("*** Employee Details ***"); System.out.println("Employee Id : "+employee.getId()); System.out.println("Employee Name : "+employee.getName()); System.out.println("Employee Age : "+employee.getAge()); System.out.println("Department : "+employee.getDept()); //Commit the changes session.getTransaction().commit(); //Close the session session.close(); } }
Output
INFO: HHH000261: Table found: XBBLKRJ.EMPLOYEE
Apr 13, 2017 5:13:09 PM org.hibernate.tool.hbm2ddl.TableMetadata
INFO: HHH000037: Columns: [id, age, name, dept]
Apr 13, 2017 5:13:09 PM org.hibernate.tool.hbm2ddl.TableMetadata
INFO: HHH000108: Foreign keys: []
Apr 13, 2017 5:13:09 PM org.hibernate.tool.hbm2ddl.TableMetadata
INFO: HHH000126: Indexes: [sys_c0015506]
Apr 13, 2017 5:13:09 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: select employee0_.id as id1_0_0_, employee0_.AGE as AGE2_0_0_, employee0_.DEPT as DEPT3_0_0_, employee0_.NAME as NAME4_0_0_ from EMPLOYEE employee0_ where employee0_.id=?
*** Employee Details ***
Employee Id : 413
Employee Name : JIP
Employee Age : 100
Department : IT
- In the above code, we have used session.get() to retrieve the employee with the ID 413, Hibernate immediately hits the database and returns the original Employee object. This is the reason, we are getting the below SQL query immediately when we retrieve the employee id (employee.getId()).
Hibernate: select employee0_.id as id1_0_0_, employee0_.AGE as AGE2_0_0_, employee0_.DEPT as DEPT3_0_0_, employee0_.NAME as NAME4_0_0_ from EMPLOYEE employee0_ where employee0_.id=?
Finally, we can summarize the difference between session.get() and session.load() in Hibernate as below
session.get() | session.load() |
---|---|
Never returns a proxy object | Always returns the proxy object |
Returns null when the corresponding record is not found but the execution continues | Throws ObjectNotFoundException when the corresponding record is not found and executions terminates |
Eager Loading, as it hits the database immediately and returns the original object | Lazy Loading, as it hits the database only when it tries to retrieve other properties of the object |
Commonly used for retrieving (select) the data | Commonly used for delete, update operations |
prasad m says
excellent . . . . . . .