Monday, May 12, 2014

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.

No comments: