Showing posts with label hibernate interview questions. Show all posts
Showing posts with label hibernate interview questions. Show all posts

Monday, May 12, 2014

Difference between load() and get() methods in Hibernate

Difference between load() and get() methods in Hibernate


Dont use load() when you are not sure about the object exists/record exists in database.
When you call load() method if the object is not exist/unique id is not found in DB, then it will throw an exception.

When you call get() method if the object is not exist then it will return NULL reference.


Hibernate persisting transient object to database - Equality and Identity of persistent object

Hibernate persisting transient object to database How it behaves?.

It is not appropriate to save an object that has already been persisted. Equally, it is not
appropriate to update a transient object. If it is impossible or inconvenient to determine the
state of the object from your application code, you may use the saveOrUpdate() method.

When we have a persistent object in Hibernate,
that object represents both an instance of a class in a particular Java virtual machine (JVM)
and a row (or rows) in a database table (or tables).
Requesting a persistent object again from the same Hibernate session returns the same
Java instance of a class, which means that you can compare the objects using the standard Java
== equality syntax. If, however, you request a persistent object from more than one Hibernate
session, Hibernate will provide distinct instances from each session, and the == operator will
return false if you compare these object instances.