Auto Generated Values

Marking a field with the @GeneratedValuejakarta.persistence.GeneratedValue - JPA Annotation Specifies a generation strategy for generated primary keys. annotation specifies that a value will be automatically generated for that field. This is primarily intended for primary key fields but ObjectDB also supports this annotation for non-key numeric persistent fields as well. Several different value generation strategies can be used as explained below.

The Auto Strategy

ObjectDB maintains a special global number generator for every database. This number generator is used to generate automatic object IDs for entity objects with no primary key fields defined (as explained in the previous section).

The same number generator is also used to generate numeric values for primary key fields annotated by @GeneratedValuejakarta.persistence.GeneratedValue - JPA Annotation Specifies a generation strategy for generated primary keys. with the AUTOjakarta.persistence.GenerationType.AUTO - JPA Enum Constant Indicates that the persistence provider should pick an appropriate strategy for the particular database. strategy:

@Entityjakarta.persistence.Entity - JPA Annotation
 Declares that the annotated class is an entity.
public class EntityWithAutoId1 {
    @Idjakarta.persistence.Id - JPA Annotation
 Identifies the primary key of an entity. @GeneratedValuejakarta.persistence.GeneratedValue - JPA Annotation
 Specifies a generation strategy for generated primary keys.(strategyjakarta.persistence.GeneratedValue.strategy - JPA Annotation Attribute
 (Optional) The primary key generation strategy that
 the persistence provider must use to generate the
 annotated entity primary key.=GenerationTypejakarta.persistence.GenerationType - JPA Enum
 Enumerates the defined primary key generation strategies..AUTOjakarta.persistence.GenerationType.AUTO - JPA Enum Constant
 Indicates that the persistence provider should pick an
 appropriate strategy for the particular database.) long id;
     :
}

AUTOjakarta.persistence.GenerationType.AUTO - JPA Enum Constant Indicates that the persistence provider should pick an appropriate strategy for the particular database. is the default strategy, so the following definition is equivalent:

@Entityjakarta.persistence.Entity - JPA Annotation
 Declares that the annotated class is an entity.
public class EntityWithAutoId2 {
    @Idjakarta.persistence.Id - JPA Annotation
 Identifies the primary key of an entity. @GeneratedValuejakarta.persistence.GeneratedValue - JPA Annotation
 Specifies a generation strategy for generated primary keys. long id;
     :
}

During a commit the AUTOjakarta.persistence.GenerationType.AUTO - JPA Enum Constant Indicates that the persistence provider should pick an appropriate strategy for the particular database. strategy uses the global number generator to generate a primary key for every new entity object. These generated values are unique at the database level and are never recycled, as explained in the previous section.

The Identity Strategy

The IDENTITYjakarta.persistence.GenerationType.IDENTITY - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database identity column. strategy is very similar to the AUTOjakarta.persistence.GenerationType.AUTO - JPA Enum Constant Indicates that the persistence provider should pick an appropriate strategy for the particular database. strategy:

@Entityjakarta.persistence.Entity - JPA Annotation
 Declares that the annotated class is an entity.
public class EntityWithIdentityId {
    @Idjakarta.persistence.Id - JPA Annotation
 Identifies the primary key of an entity. @GeneratedValuejakarta.persistence.GeneratedValue - JPA Annotation
 Specifies a generation strategy for generated primary keys.(strategyjakarta.persistence.GeneratedValue.strategy - JPA Annotation Attribute
 (Optional) The primary key generation strategy that
 the persistence provider must use to generate the
 annotated entity primary key.=GenerationTypejakarta.persistence.GenerationType - JPA Enum
 Enumerates the defined primary key generation strategies..IDENTITYjakarta.persistence.GenerationType.IDENTITY - JPA Enum Constant
 Indicates that the persistence provider must assign
 primary keys for the entity using a database identity
 column.) long id;
     :
}

The IDENTITYjakarta.persistence.GenerationType.IDENTITY - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database identity column. strategy also generates an automatic value during commit for every new entity object. The difference is that a separate identity generator is managed per type hierarchy, so generated values are unique only per type hierarchy.

The Sequence Strategy

The sequence strategy consists of two parts - defining a named sequence and using the named sequence in one or more fields in one or more classes. The @SequenceGeneratorjakarta.persistence.SequenceGenerator - JPA Annotation Defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. annotation is used to define a sequence and accepts a name, an initial value (the default is 1) and an allocation size (the default is 50). A sequence is global to the application and can be used by one or more fields in one or more classes. The SEQUENCEjakarta.persistence.GenerationType.SEQUENCE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy is used in the @GeneratedValuejakarta.persistence.GeneratedValue - JPA Annotation Specifies a generation strategy for generated primary keys. annotation to attach the given field to the previously defined named sequence:

@Entityjakarta.persistence.Entity - JPA Annotation
 Declares that the annotated class is an entity.
// Define a sequence - might also be in another class:
@SequenceGeneratorjakarta.persistence.SequenceGenerator - JPA Annotation
 Defines a primary key generator that may be referenced by name when
 a generator element is specified for the GeneratedValue
 annotation.(namejakarta.persistence.SequenceGenerator.name - JPA Annotation Attribute
 (Optional) A unique generator name that can be referenced
 by one or more classes to be the generator for primary key
 values.="seq", initialValuejakarta.persistence.SequenceGenerator.initialValue - JPA Annotation Attribute
 (Optional) The value from which the sequence object
 is to start generating.=1, allocationSizejakarta.persistence.SequenceGenerator.allocationSize - JPA Annotation Attribute
 (Optional) The amount to increment by when allocating
 sequence numbers from the sequence.=100)
public class EntityWithSequenceId {
    // Use the sequence that is defined above:
    @GeneratedValuejakarta.persistence.GeneratedValue - JPA Annotation
 Specifies a generation strategy for generated primary keys.(strategyjakarta.persistence.GeneratedValue.strategy - JPA Annotation Attribute
 (Optional) The primary key generation strategy that
 the persistence provider must use to generate the
 annotated entity primary key.=GenerationTypejakarta.persistence.GenerationType - JPA Enum
 Enumerates the defined primary key generation strategies..SEQUENCEjakarta.persistence.GenerationType.SEQUENCE - JPA Enum Constant
 Indicates that the persistence provider must assign
 primary keys for the entity using a database sequence., generatorjakarta.persistence.GeneratedValue.generator - JPA Annotation Attribute
 (Optional) The name of the primary key generator to
 use, as specified by the SequenceGenerator
 or TableGenerator annotation which declares
 the generator.="seq")
    @Idjakarta.persistence.Id - JPA Annotation
 Identifies the primary key of an entity. long id;
}

Unlike AUTOjakarta.persistence.GenerationType.AUTO - JPA Enum Constant Indicates that the persistence provider should pick an appropriate strategy for the particular database. and IDENTITYjakarta.persistence.GenerationType.IDENTITY - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database identity column., the SEQUENCEjakarta.persistence.GenerationType.SEQUENCE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy generates an automatic value as soon as a new entity object is persisted (i.e. before commit). This may be useful when the primary key value is needed earlier. To minimize round trips to the database server, IDs are allocated in groups. The number of IDs in each allocation is specified by the allocationSizejakarta.persistence.SequenceGenerator.allocationSize - JPA Annotation Attribute (Optional) The amount to increment by when allocating sequence numbers from the sequence. attribute. It is possible that some of the IDs in a given allocation will not be used. Therefore, this strategy does not guarantee there will be no gaps in sequence values.

The Table Strategy

The TABLEjakarta.persistence.GenerationType.TABLE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness. strategy is very similar to the SEQUENCEjakarta.persistence.GenerationType.SEQUENCE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy:

@Entityjakarta.persistence.Entity - JPA Annotation
 Declares that the annotated class is an entity.
@TableGeneratorjakarta.persistence.TableGenerator - JPA Annotation
 Defines a primary key generator that may be referenced
 by name when a generator element is specified for the
 GeneratedValue annotation.(namejakarta.persistence.TableGenerator.name - JPA Annotation Attribute
 (optional) A unique generator name that can be referenced
 by one or more classes to be the generator for id values.="tab", initialValuejakarta.persistence.TableGenerator.initialValue - JPA Annotation Attribute
 (Optional) The initial value to be used to initialize the
 column that stores the last value generated.=0, allocationSizejakarta.persistence.TableGenerator.allocationSize - JPA Annotation Attribute
 (Optional) The amount to increment by when allocating id
 numbers from the generator.=50)
public class EntityWithTableId {
    @GeneratedValuejakarta.persistence.GeneratedValue - JPA Annotation
 Specifies a generation strategy for generated primary keys.(strategyjakarta.persistence.GeneratedValue.strategy - JPA Annotation Attribute
 (Optional) The primary key generation strategy that
 the persistence provider must use to generate the
 annotated entity primary key.=GenerationTypejakarta.persistence.GenerationType - JPA Enum
 Enumerates the defined primary key generation strategies..TABLEjakarta.persistence.GenerationType.TABLE - JPA Enum Constant
 Indicates that the persistence provider must assign
 primary keys for the entity using an underlying
 database table to ensure uniqueness., generatorjakarta.persistence.GeneratedValue.generator - JPA Annotation Attribute
 (Optional) The name of the primary key generator to
 use, as specified by the SequenceGenerator
 or TableGenerator annotation which declares
 the generator.="tab")
    @Idjakarta.persistence.Id - JPA Annotation
 Identifies the primary key of an entity. long id;
}

ORM-based JPA providers (such as Hibernate, TopLink, EclipseLink, OpenJPA, JPOX, etc.) simulate a sequence using a table to support this strategy. ObjectDB does not have tables, so the TABLEjakarta.persistence.GenerationType.TABLE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness. and SEQUENCEjakarta.persistence.GenerationType.SEQUENCE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database sequence. strategies are almost identical.

A tiny difference is related to the initial value attribute. Whereas the SEQUENCEjakarta.persistence.GenerationType.SEQUENCE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy maintains the next sequence number to be used the TABLEjakarta.persistence.GenerationType.TABLE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness. strategy maintains the last value that was used. The implication for the initialValue attribute is that if you want sequence numbers to start with 1 in the TABLEjakarta.persistence.GenerationType.TABLE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness. strategy initialValue=0 has to be specified in the @SequenceGeneratorjakarta.persistence.SequenceGenerator - JPA Annotation Defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. annotation.