Skip to main content

Hibernate - Identity


Identity is the fact that being who or what a thing is.

In Java two objects are identical if they are referring to the same memory location.

object1 == object2

Equals means that they have the same value but may not have same memory location.


In relational databases, objects are identical if they share the same table and have same primary key. This is known as database identity in Java side.

private Long id;

  • Because @Id on the field, hibernate use field access.
  • If we don't provide @GeneratedValue, JPA provider doesn't provide a key for us. We have to assign ourselves. These are called application-assigned identifiers.

Candidate Key

Column(s) we could use to identify a database row is called candidate keys.

  • Never null.
  • Unique.
  • Never changes.

Natural Key

A key with a business meaning is called a natural key. For an Employee table can have a employee number to represent employee's identity.
Practically, this will have side effects when the system grows.
- composite natural keys are keys combining two or more columns.

Synthetic Key / Surrogate Key

A unique, not null column generated by the database or the application with no business meaning is called surrogate key.
This is the preferred option.