Auto Generated Values
Marking a field with the @GeneratedValuejavax.persistence.GeneratedValue - JPA Annotation Provides 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 StrategyThe Identity StrategyThe Sequence StrategyThe Table StrategyThe 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 @GeneratedValuejavax.persistence.GeneratedValue - JPA Annotation Provides for the specification of generation strategies for the values of primary keys. with the AUTOjavax.persistence.GenerationType.AUTO - JPA Enum Constant Indicates that the persistence provider should pick an appropriate strategy for the particular database. strategy:
@Entityjavax.persistence.Entity - JPA Annotation Specifies that the class is an entity. public class EntityWithAutoId1 { @Idjavax.persistence.Id - JPA Annotation Specifies the primary key of an entity. @GeneratedValuejavax.persistence.GeneratedValue - JPA Annotation Provides 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 Enum Defines the types of primary key generation strategies..AUTOjavax.persistence.GenerationType.AUTO - JPA Enum Constant Indicates that the persistence provider should pick an appropriate strategy for the particular database.) long id; : }
AUTOjavax.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:
@Entityjavax.persistence.Entity - JPA Annotation Specifies that the class is an entity. public class EntityWithAutoId2 { @Idjavax.persistence.Id - JPA Annotation Specifies the primary key of an entity. @GeneratedValuejavax.persistence.GeneratedValue - JPA Annotation Provides for the specification of generation strategies for the values of primary keys. long id; : }
During a commit the AUTOjavax.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 IDENTITYjavax.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 AUTOjavax.persistence.GenerationType.AUTO - JPA Enum Constant Indicates that the persistence provider should pick an appropriate strategy for the particular database. strategy:
@Entityjavax.persistence.Entity - JPA Annotation Specifies that the class is an entity. public class EntityWithIdentityId { @Idjavax.persistence.Id - JPA Annotation Specifies the primary key of an entity. @GeneratedValuejavax.persistence.GeneratedValue - JPA Annotation Provides 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 Enum Defines the types of primary key generation strategies..IDENTITYjavax.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 IDENTITYjavax.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 @SequenceGeneratorjavax.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 SEQUENCEjavax.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 @GeneratedValuejavax.persistence.GeneratedValue - JPA Annotation Provides 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 Annotation Specifies that the class is an entity. // Define a sequence - might also be in another class: @SequenceGeneratorjavax.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.(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 Annotation Provides 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 Enum Defines the types of primary key generation strategies..SEQUENCEjavax.persistence.GenerationType.SEQUENCE - JPA Enum Constant Indicates 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 SequenceGenerator or TableGenerator annotation.="seq") @Idjavax.persistence.Id - JPA Annotation Specifies the primary key of an entity. long id; }
Unlike AUTOjavax.persistence.GenerationType.AUTO - JPA Enum Constant Indicates that the persistence provider should pick an appropriate strategy for the particular database. and IDENTITYjavax.persistence.GenerationType.IDENTITY - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database identity column., the SEQUENCEjavax.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 allocationSizejavax.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 TABLEjavax.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 SEQUENCEjavax.persistence.GenerationType.SEQUENCE - JPA Enum Constant Indicates that the persistence provider must assign primary keys for the entity using a database sequence. strategy:
@Entityjavax.persistence.Entity - JPA Annotation Specifies that the class is an entity. @TableGeneratorjavax.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.(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 Annotation Provides 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 Enum Defines the types of primary key generation strategies..TABLEjavax.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., generatorjavax.persistence.GeneratedValue.generator - JPA Annotation Attribute (Optional) The name of the primary key generator to use as specified in the SequenceGenerator or TableGenerator annotation.="tab") @Idjavax.persistence.Id - JPA Annotation Specifies 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 Constant Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness. and SEQUENCEjavax.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 SEQUENCEjavax.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 TABLEjavax.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 TABLEjavax.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 @SequenceGeneratorjavax.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.