@Identity

With @Identity we can specify additional attributes for database identity and sequences and we can additionally apply this on the class level or on the property level.

It is useful to be able to specify @Identity on the class level when using @MappedBy and the @Id property is inherited.

The additional properties we can specify include:

  • start - start value
  • cache - cache value (performance optimisation)
  • generatedBy - By DEFAULT or ALWAYS

 

Example use on class

@Identity(generated = BY_DEFAULT, start = 1000, cache = 100)
@Entity
public class AuditLog {
  ...

Example use on property

@Entity
public class AuditLog {

  @Id
  @Identity(generated = BY_DEFAULT, start = 10000, cache = 1000)
  long id;
  ...

 

start value

The start value is supported in DDL generation for H2, Postgres, Oracle and SQL Server sequences.

 

cache value

The cache value is an important performance optimisation available in some databases for tables that will have a lot of inserts. This specifies how many identity values the database should generate and cache ahead of time reducing the internal locking the database uses. Databases supporting this include Postgres, Oracle & NuoDB.