CodesJava

Easy learning with example program codes

What is difference between Hibernate Session get() and load() method?


session.load()

• It will always return a “proxy” in Hibernate terms without hitting the database. In Hibernate, proxy represents a fake object with the given identifier value but its properties are not initialized yet.
• If no record found, it will throws an ObjectNotFoundException.
• Example: Let we call session.load(Employee.class,new Integer(10)). Hibernate will create one fake Employee object [row] in the memory with id 10, but other properties of Employee class will not even be initialized. When we try to retrieve other properties of Employee object, it will hit the database and search the record with employee id 10 and fetch the values. It will throws ObjectNotFoundException in case when no record found.

session.get()

• It always hit the database and return the real object, an object that represent the database record not proxy.
• It return null if no record found.
• It will not throw any exception in case when no record found.

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki