Pages

Object identity vs Object equality

Object identity vs Object equality

Object identity (==) is defined by the JVM, Two object references are identical if they point to the same memory location in the JVM.

Object equality is defined by the classes that implement equals() method, Two different objects have the same value. This concept sometimes called as equivalence.

Object stored in the relational database are identical If they represents the same row or they share the same identifier values. This concept is known as database identity.

Handling database identity
Hibernate expose database identity in two ways
(1) value of a identifier property of a persistent instance.
(2) value returned by session.getIdentifier(Object entity).

Entities and Value Type object in Hibernate

Entities and Value Type object in Hibernate


Entity type objects has its own life cycle, object reference of an entity type is stored as a reference (as a foreign key value) in the database. Entity object has its own database identity. It can have shared references with the other entity type object. Its may exist independently of any other entity.

Values type object do not have life cycle, it belongs to entity type object and its persisted state is stored in the table row of same entity type object. Value type object do not have identity value or identifier properties. Life of a value type object is bounded by its lifespan of owning entity type object. Values type object do not have shared references. Values type object are like String class , Integer class etc. User Type class can also be mapped as a value types.