Difference between session.get() vs session.load() in hibernate ?
Table of Contents
Difference between session.get() vs session.load() in Hibernate ?
Both are used to retrieve the objects from the database.
Differences between session.get() vs session.load()
- session.load() will return the hibernate proxy object, but session.get() will return real object.
- session.load() will not hit the database, but session.get() will hit the database all the time.
- session.load() will throw object not found exception when no row found, but session.get() will return null when no row found in db.
session.get() Sample syntax & example:
session.get(arg0, arg1)
Example:
session.get(Author.class,2);
session.load() sample syntax & example:
session.load(arg0, arg1)
Example:
session.load(Author.class,2);
Important Note:
Author is the hibernate mapping class and Integer value 2 will be passed to the primary key field of the Author class and respective objects will be retrieved from the database.
session.load() and session.get() are overloaded methods, just for understanding I mentioned two argument method above.
session.get() vs session.load() when no row found / Handling exceptions
1. When no row found then session.get() will return null
2. session.load() will return the unrecoverable object not found exception.
[Exception in thread “main” org.hibernate.ObjectNotFoundException:No row with the given identifier exists]
When to use what (sessin.get() [or] session.load() ] ?
when you know that data exist surely in db then you can go with session.load() because performance will be far better than session.get() because it will not hit the database.
Also when you are not sure about the data existence in the db then better go with session.get() since it will return only null when no rows found rather than returning objectnotfound exception.
Code Samples:
Main class to run and understand :
package in.javadomain; import org.hibernate.Session; public class LoadVsGet { public static void main(String[] args) { // creating the session from the sessionfactory Session session = HibernateConfig.getSessionFactory().openSession(); // fetching session.beginTransaction(); Author authorFetch = (Author)session.get(Author.class, 2); System.out.println(authorFetch.getAuthorName()); System.out.println(authorFetch.getSiteName()); session.getTransaction().commit(); // closing the session factory session session.close(); HibernateConfig.getSessionFactory().close(); } }
Author Hibernate mapping class: [It’s just a plain POJO]
package in.javadomain; public class Author { private int authorID; private String siteName; private String authorName; public int getAuthorID() { return authorID; } public void setAuthorID(int authorID) { this.authorID = authorID; } public String getSiteName() { return siteName; } public void setSiteName(String siteName) { this.siteName = siteName; } public String getAuthorName() { return authorName; } public void setAuthorName(String authorName) { this.authorName = authorName; } }
Hibernate SessionFactory:
package in.javadomain; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; @SuppressWarnings("deprecation") public class HibernateConfig { private static SessionFactory sessionFactory; static { try{ Configuration configuration = new Configuration().configure(); sessionFactory = configuration.buildSessionFactory(); }catch(HibernateException e){ System.out.println("Problem in hibernate creation!"); } } public static SessionFactory getSessionFactory(){ return sessionFactory; } }
Hibernate Mapping file: [hibernate configuration xml file]
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">DB_PASSWORD</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property> <property name="hibernate.connection.username">MYSQL_USERNAME</property> <property name="hibernate.default_schema">DATABASE_NAME</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="in/javadomain/Author.hbm.xml"/> </session-factory> </hibernate-configuration>
Author.hbm.xml: [Mapping file to map java objects with relational database]
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Sep 9, 2015 9:28:40 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="in.javadomain.Author" table="AUTHOR"> <id name="authorID" type="int"> <column name="ID" /> <generator class="identity" /> </id> <property name="siteName" type="java.lang.String"> <column name="SITENAME" /> </property> <property name="authorName" type="java.lang.String"> <column name="AUTHORNAME" /> </property> </class> </hibernate-mapping>
Required Hibernate 4 Jars can be downloaded from here,
Hibernate 4 Required Jars including MySql connector J Jar
Like to appreciate/comment, please do it below…