Auto Generated Values
Marking a field with the @GeneratedValue
javax.persistence.GeneratedValue - JPA AnnotationProvides for the specification of generation strategies for the values of 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.
This page covers the following topics:
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 @GeneratedValue
javax.persistence.GeneratedValue - JPA AnnotationProvides for the specification of generation strategies for the values of primary keys. with the AUTO
javax.persistence.GenerationType.AUTO - JPA Enum ConstantIndicates that the persistence provider should pick an appropriate strategy for the particular database. strategy:
@Entityjavax.persistence.Entity - JPA AnnotationSpecifies that the class is an entity. public class EntityWithAutoId1 { @Idjavax.persistence.Id - JPA AnnotationSpecifies the primary key of an entity. @GeneratedValuejavax.persistence.GeneratedValue - JPA AnnotationProvides for the specification of generation strategies for the values of primary keys.(strategyjavax.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.=GenerationTypejavax.persistence.GenerationType - JPA EnumDefines the types of primary key generation strategies..AUTOjavax.persistence.GenerationType.AUTO - JPA Enum ConstantIndicates that the persistence provider should pick an appropriate strategy for the particular database.) long id; : }
AUTO
javax.persistence.GenerationType.AUTO - JPA Enum ConstantIndicates that the persistence provider should pick an appropriate strategy for the particular database. is the default strategy, so the following definition is equivalent:
@Entityjavax.persistence.Entity - JPA AnnotationSpecifies that the class is an entity. public class EntityWithAutoId2 { @Idjavax.persistence.Id - JPA AnnotationSpecifies the primary key of an entity. @GeneratedValuejavax.persistence.GeneratedValue - JPA AnnotationProvides for the specification of generation strategies for the values of primary keys. long id; : }
During a commit the AUTO
javax.persistence.GenerationType.AUTO - JPA Enum ConstantIndicates 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 IDENTITY
javax.persistence.GenerationType.IDENTITY - JPA Enum ConstantIndicates that the persistence provider must assign primary keys for the entity using a database identity column. strategy is very similar to the AUTO
javax.persistence.GenerationType.AUTO - JPA Enum ConstantIndicates that the persistence provider should pick an appropriate strategy for the particular database. strategy:
@Entityjavax.persistence.Entity - JPA AnnotationSpecifies that the class is an entity. public class EntityWithIdentityId { @Idjavax.persistence.Id - JPA AnnotationSpecifies the primary key of an entity. @GeneratedValuejavax.persistence.GeneratedValue - JPA AnnotationProvides for the specification of generation strategies for the values of primary keys.(strategyjavax.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.=GenerationTypejavax.persistence.GenerationType - JPA EnumDefines the types of primary key generation strategies..IDENTITYjavax.persistence.GenerationType.IDENTITY - JPA Enum ConstantIndicates that the persistence provider must assign primary keys for the entity using a database identity column.) long id; : }
The IDENTITY
javax.persistence.GenerationType.IDENTITY - JPA Enum ConstantIndicates 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 @SequenceGenerator
javax.persistence.SequenceGenerator - JPA AnnotationDefines a primary key generator that may be referenced by name when a generator element is specified for the {@link 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 SEQUENCE
javax.persistence.GenerationType.SEQUENCE - JPA Enum ConstantIndicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy is used in the @GeneratedValue
javax.persistence.GeneratedValue - JPA AnnotationProvides for the specification of generation strategies for the values of primary keys. annotation to attach the given field to the previously defined named sequence:
@Entityjavax.persistence.Entity - JPA AnnotationSpecifies that the class is an entity. // Define a sequence - might also be in another class: @SequenceGeneratorjavax.persistence.SequenceGenerator - JPA AnnotationDefines a primary key generator that may be referenced by name when a generator element is specified for the {@link GeneratedValue} annotation.(namejavax.persistence.SequenceGenerator.name - JPA Annotation Attribute(Required) A unique generator name that can be referenced by one or more classes to be the generator for primary key values.="seq", initialValuejavax.persistence.SequenceGenerator.initialValue - JPA Annotation Attribute(Optional) The value from which the sequence object is to start generating.=1, allocationSizejavax.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: @GeneratedValuejavax.persistence.GeneratedValue - JPA AnnotationProvides for the specification of generation strategies for the values of primary keys.(strategyjavax.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.=GenerationTypejavax.persistence.GenerationType - JPA EnumDefines the types of primary key generation strategies..SEQUENCEjavax.persistence.GenerationType.SEQUENCE - JPA Enum ConstantIndicates that the persistence provider must assign primary keys for the entity using a database sequence., generatorjavax.persistence.GeneratedValue.generator - JPA Annotation Attribute(Optional) The name of the primary key generator to use as specified in the {@link SequenceGenerator} or {@link TableGenerator} annotation.="seq") @Idjavax.persistence.Id - JPA AnnotationSpecifies the primary key of an entity. long id; }
Unlike AUTO
javax.persistence.GenerationType.AUTO - JPA Enum ConstantIndicates that the persistence provider should pick an appropriate strategy for the particular database. and IDENTITY
javax.persistence.GenerationType.IDENTITY - JPA Enum ConstantIndicates that the persistence provider must assign primary keys for the entity using a database identity column., the SEQUENCE
javax.persistence.GenerationType.SEQUENCE - JPA Enum ConstantIndicates 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 allocationSize
javax.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 TABLE
javax.persistence.GenerationType.TABLE - JPA Enum ConstantIndicates 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 SEQUENCE
javax.persistence.GenerationType.SEQUENCE - JPA Enum ConstantIndicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy:
@Entityjavax.persistence.Entity - JPA AnnotationSpecifies that the class is an entity. @TableGeneratorjavax.persistence.TableGenerator - JPA AnnotationDefines a primary key generator that may be referenced by name when a generator element is specified for the {@link GeneratedValue} annotation.(namejavax.persistence.TableGenerator.name - JPA Annotation Attribute(Required) A unique generator name that can be referenced by one or more classes to be the generator for id values.="tab", initialValuejavax.persistence.TableGenerator.initialValue - JPA Annotation Attribute(Optional) The initial value to be used to initialize the column that stores the last value generated.=0, allocationSizejavax.persistence.TableGenerator.allocationSize - JPA Annotation Attribute(Optional) The amount to increment by when allocating id numbers from the generator.=50) public class EntityWithTableId { @GeneratedValuejavax.persistence.GeneratedValue - JPA AnnotationProvides for the specification of generation strategies for the values of primary keys.(strategyjavax.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.=GenerationTypejavax.persistence.GenerationType - JPA EnumDefines the types of primary key generation strategies..TABLEjavax.persistence.GenerationType.TABLE - JPA Enum ConstantIndicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness., generatorjavax.persistence.GeneratedValue.generator - JPA Annotation Attribute(Optional) The name of the primary key generator to use as specified in the {@link SequenceGenerator} or {@link TableGenerator} annotation.="tab") @Idjavax.persistence.Id - JPA AnnotationSpecifies 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 TABLEjavax.persistence.GenerationType.TABLE - JPA Enum ConstantIndicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness. and SEQUENCE
javax.persistence.GenerationType.SEQUENCE - JPA Enum ConstantIndicates 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 SEQUENCE
javax.persistence.GenerationType.SEQUENCE - JPA Enum ConstantIndicates 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 TABLEjavax.persistence.GenerationType.TABLE - JPA Enum ConstantIndicates 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 TABLEjavax.persistence.GenerationType.TABLE - JPA Enum ConstantIndicates 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 @SequenceGenerator
javax.persistence.SequenceGenerator - JPA AnnotationDefines a primary key generator that may be referenced by name when a generator element is specified for the {@link GeneratedValue} annotation. annotation.