Auto Generated Values
Marking a field with the
@GeneratedValuejakarta.persistence.GeneratedValueSpecifies a generation strategy for generated primary keys. annotation indicates that the field's value is automatically generated. This annotation is primarily for primary key fields, but ObjectDB also supports it for non-key numeric persistent fields. The following sections explain the available value generation strategies.
This page covers the following topics:
The AUTO strategyThe IDENTITY strategyThe SEQUENCE StrategyThe Table StrategyThe AUTO strategy
ObjectDB maintains a global number generator for each database. This generator creates automatic object IDs for entities that do not have defined primary key fields (as explained in the previous section) and for primary key fields that are annotated with @GeneratedValuejakarta.persistence.GeneratedValueSpecifies a generation strategy for generated primary keys. and use the AUTOjakarta.persistence.GenerationType.AUTOIndicates that the persistence provider should pick an appropriate strategy for the particular database. strategy:
@Entityjakarta.persistence.EntityDeclares that the annotated class is an entity. public class EntityWithAutoId1 { @Idjakarta.persistence.IdIdentifies the primary key of an entity. @GeneratedValuejakarta.persistence.GeneratedValueSpecifies a generation strategy for generated primary keys.(strategyjakarta.persistence.GeneratedValue.strategy(Optional) The primary key generation strategy that the persistence provider must use to generate the annotated entity primary key.=GenerationTypejakarta.persistence.GenerationTypeEnumerates the defined primary key generation strategies..AUTOjakarta.persistence.GenerationType.AUTOIndicates that the persistence provider should pick an appropriate strategy for the particular database.) long id; : }
AUTOjakarta.persistence.GenerationType.AUTOIndicates 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.EntityDeclares that the annotated class is an entity. public class EntityWithAutoId2 { @Idjakarta.persistence.IdIdentifies the primary key of an entity. @GeneratedValuejakarta.persistence.GeneratedValueSpecifies a generation strategy for generated primary keys. long id; : }
When a transaction is committed, the AUTOjakarta.persistence.GenerationType.AUTOIndicates 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 each new entity. 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.IDENTITYIndicates 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.AUTOIndicates that the persistence provider should pick an appropriate strategy for the particular database. strategy:
@Entityjakarta.persistence.EntityDeclares that the annotated class is an entity. public class EntityWithIdentityId { @Idjakarta.persistence.IdIdentifies the primary key of an entity. @GeneratedValuejakarta.persistence.GeneratedValueSpecifies a generation strategy for generated primary keys.(strategyjakarta.persistence.GeneratedValue.strategy(Optional) The primary key generation strategy that the persistence provider must use to generate the annotated entity primary key.=GenerationTypejakarta.persistence.GenerationTypeEnumerates the defined primary key generation strategies..IDENTITYjakarta.persistence.GenerationType.IDENTITYIndicates that the persistence provider must assign primary keys for the entity using a database identity column.) long id; : }
The IDENTITYjakarta.persistence.GenerationType.IDENTITYIndicates that the persistence provider must assign primary keys for the entity using a database identity column. strategy also generates an automatic value for each new entity during the commit operation. The difference is that IDENTITY manages a separate generator for each type hierarchy, so the generated values are unique only within that hierarchy.
The SEQUENCE Strategy
The sequence strategy has two parts: defining a named sequence and then using it in one or more fields. Use the @SequenceGeneratorjakarta.persistence.SequenceGeneratorDefines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. annotation to define a sequence. It takes a name, an initial value (default is 1), and an allocation size (default is 50). A sequence is global to the application and can be used by multiple fields across different classes. Specify the SEQUENCEjakarta.persistence.GenerationType.SEQUENCEIndicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy in the @GeneratedValuejakarta.persistence.GeneratedValueSpecifies a generation strategy for generated primary keys. annotation to attach the field to a previously defined named sequence:
@Entityjakarta.persistence.EntityDeclares that the annotated class is an entity. // Define a sequence - might also be in another class: @SequenceGeneratorjakarta.persistence.SequenceGeneratorDefines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation.(namejakarta.persistence.SequenceGenerator.name(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(Optional) The value from which the sequence object is to start generating.=1, allocationSizejakarta.persistence.SequenceGenerator.allocationSize(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.GeneratedValueSpecifies a generation strategy for generated primary keys.(strategyjakarta.persistence.GeneratedValue.strategy(Optional) The primary key generation strategy that the persistence provider must use to generate the annotated entity primary key.=GenerationTypejakarta.persistence.GenerationTypeEnumerates the defined primary key generation strategies..SEQUENCEjakarta.persistence.GenerationType.SEQUENCEIndicates that the persistence provider must assign primary keys for the entity using a database sequence., generatorjakarta.persistence.GeneratedValue.generator(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.IdIdentifies the primary key of an entity. long id; }
Unlike AUTOjakarta.persistence.GenerationType.AUTOIndicates that the persistence provider should pick an appropriate strategy for the particular database. and IDENTITYjakarta.persistence.GenerationType.IDENTITYIndicates that the persistence provider must assign primary keys for the entity using a database identity column., the SEQUENCEjakarta.persistence.GenerationType.SEQUENCEIndicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy generates a value as soon as a new entity is persisted (that is, before the commit). This is useful when you need the primary key value earlier. To minimize round trips to the database server, IDs are allocated in groups. The allocationSizejakarta.persistence.SequenceGenerator.allocationSize(Optional) The amount to increment by when allocating sequence numbers from the sequence. attribute specifies the number of IDs in each allocation. Some IDs in an allocation might not be used. Therefore, this strategy can result in gaps in the sequence values.nd
The Table Strategy
The TABLEjakarta.persistence.GenerationType.TABLEIndicates 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.SEQUENCEIndicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy:
@Entityjakarta.persistence.EntityDeclares that the annotated class is an entity. @TableGeneratorjakarta.persistence.TableGeneratorDefines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation.(namejakarta.persistence.TableGenerator.name(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(Optional) The initial value to be used to initialize the column that stores the last value generated.=0, allocationSizejakarta.persistence.TableGenerator.allocationSize(Optional) The amount to increment by when allocating id numbers from the generator.=50) public class EntityWithTableId { @GeneratedValuejakarta.persistence.GeneratedValueSpecifies a generation strategy for generated primary keys.(strategyjakarta.persistence.GeneratedValue.strategy(Optional) The primary key generation strategy that the persistence provider must use to generate the annotated entity primary key.=GenerationTypejakarta.persistence.GenerationTypeEnumerates the defined primary key generation strategies..TABLEjakarta.persistence.GenerationType.TABLEIndicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness., generatorjakarta.persistence.GeneratedValue.generator(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.IdIdentifies the primary key of an entity. long id; }
ORM-based JPA providers (such as Hibernate, TopLink, EclipseLink, and OpenJPA) simulate a sequence by using a table to support this strategy. ObjectDB does not have tables, so the TABLEjakarta.persistence.GenerationType.TABLEIndicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.and SEQUENCEjakarta.persistence.GenerationType.SEQUENCEIndicates that the persistence provider must assign primary keys for the entity using a database sequence. strategies are almost identical.
A minor difference relates to the initial value attribute. Whereas the SEQUENCEjakarta.persistence.GenerationType.SEQUENCEIndicates 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.TABLEIndicates 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. This means that if you want sequence numbers to start with 1 when using the TABLEjakarta.persistence.GenerationType.TABLEIndicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness. strategy, you must specify initialValue=0 in the @TableGeneratorjakarta.persistence.SequenceGeneratorDefines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. annotation.