ObjectDB ObjectDB

Auto Generated Values

Marking a field with the @GeneratedValuejavax.persistence.GeneratedValueJPA annotationProvides for the specification of generation strategies for the values of primary keys.See JavaDoc Reference Page... 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 @GeneratedValuejavax.persistence.GeneratedValueJPA annotationProvides for the specification of generation strategies for the values of primary keys.See JavaDoc Reference Page... with the AUTOGenerationType.AUTOenum constantIndicates that the persistence provider should pick an appropriate strategy for the particular database.See JavaDoc Reference Page... strategy:

@Entityjavax.persistence.EntityJPA annotationSpecifies that the class is an entity.See JavaDoc Reference Page...
public class EntityWithAutoId1 {
    @Idjavax.persistence.IdJPA annotationSpecifies the primary key of an entity.See JavaDoc Reference Page... @GeneratedValuejavax.persistence.GeneratedValueJPA annotationProvides for the specification of generation strategies for the
 values of primary keys.See JavaDoc Reference Page...(strategyGeneratedValue.strategyannotation element(Optional) The primary key generation strategy
 that the persistence provider must use to
 generate the annotated entity primary key.See JavaDoc Reference Page...=GenerationTypejavax.persistence.GenerationTypeJPA enumDefines the types of primary key generation strategies.See JavaDoc Reference Page....AUTOGenerationType.AUTOenum constantIndicates that the persistence provider should pick an 
 appropriate strategy for the particular database.See JavaDoc Reference Page...) long id;
     :
}

AUTOGenerationType.AUTOenum constantIndicates that the persistence provider should pick an appropriate strategy for the particular database.See JavaDoc Reference Page... is the default strategy, so the following definition is equivalent:

@Entityjavax.persistence.EntityJPA annotationSpecifies that the class is an entity.See JavaDoc Reference Page...
public class EntityWithAutoId2 {
    @Idjavax.persistence.IdJPA annotationSpecifies the primary key of an entity.See JavaDoc Reference Page... @GeneratedValuejavax.persistence.GeneratedValueJPA annotationProvides for the specification of generation strategies for the
 values of primary keys.See JavaDoc Reference Page... long id;
     :
}

During a commit the AUTOGenerationType.AUTOenum constantIndicates that the persistence provider should pick an appropriate strategy for the particular database.See JavaDoc Reference Page... 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 IDENTITYGenerationType.IDENTITYenum constantIndicates that the persistence provider must assign primary keys for the entity using a database identity column.See JavaDoc Reference Page... strategy is very similar to the AUTOGenerationType.AUTOenum constantIndicates that the persistence provider should pick an appropriate strategy for the particular database.See JavaDoc Reference Page... strategy:

@Entityjavax.persistence.EntityJPA annotationSpecifies that the class is an entity.See JavaDoc Reference Page...
public class EntityWithIdentityId {
    @Idjavax.persistence.IdJPA annotationSpecifies the primary key of an entity.See JavaDoc Reference Page... @GeneratedValuejavax.persistence.GeneratedValueJPA annotationProvides for the specification of generation strategies for the
 values of primary keys.See JavaDoc Reference Page...(strategyGeneratedValue.strategyannotation element(Optional) The primary key generation strategy
 that the persistence provider must use to
 generate the annotated entity primary key.See JavaDoc Reference Page...=GenerationTypejavax.persistence.GenerationTypeJPA enumDefines the types of primary key generation strategies.See JavaDoc Reference Page....IDENTITYGenerationType.IDENTITYenum constantIndicates that the persistence provider must assign 
 primary keys for the entity using a database identity column.See JavaDoc Reference Page...) long id;
     :
}

The IDENTITYGenerationType.IDENTITYenum constantIndicates that the persistence provider must assign primary keys for the entity using a database identity column.See JavaDoc Reference Page... 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.SequenceGeneratorJPA annotationDefines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation.See JavaDoc Reference Page... 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 SEQUENCEGenerationType.SEQUENCEenum constantIndicates that the persistence provider must assign primary keys for the entity using a database sequence.See JavaDoc Reference Page... strategy is used in the @GeneratedValuejavax.persistence.GeneratedValueJPA annotationProvides for the specification of generation strategies for the values of primary keys.See JavaDoc Reference Page... annotation to attach the given field to the previously defined named sequence:

@Entityjavax.persistence.EntityJPA annotationSpecifies that the class is an entity.See JavaDoc Reference Page...
// Define a sequence - might also be in another class:
@SequenceGeneratorjavax.persistence.SequenceGeneratorJPA annotationDefines a primary key generator that may be referenced by name when
 a generator element is specified for the GeneratedValue
 annotation.See JavaDoc Reference Page...(nameSequenceGenerator.nameannotation element(Required) A unique generator name that can be referenced 
 by one or more classes to be the generator for primary key 
 values.See JavaDoc Reference Page...="seq", initialValueSequenceGenerator.initialValueannotation element(Optional) The value from which the sequence object 
 is to start generating.See JavaDoc Reference Page...=1, allocationSizeSequenceGenerator.allocationSizeannotation element(Optional) The amount to increment by when allocating 
 sequence numbers from the sequence.See JavaDoc Reference Page...=100)
public class EntityWithSequenceId {
    // Use the sequence that is defined above:
    @GeneratedValuejavax.persistence.GeneratedValueJPA annotationProvides for the specification of generation strategies for the
 values of primary keys.See JavaDoc Reference Page...(strategyGeneratedValue.strategyannotation element(Optional) The primary key generation strategy
 that the persistence provider must use to
 generate the annotated entity primary key.See JavaDoc Reference Page...=GenerationTypejavax.persistence.GenerationTypeJPA enumDefines the types of primary key generation strategies.See JavaDoc Reference Page....SEQUENCEGenerationType.SEQUENCEenum constantIndicates that the persistence provider must assign 
 primary keys for the entity using a database sequence.See JavaDoc Reference Page..., generatorGeneratedValue.generatorannotation element(Optional) The name of the primary key generator
 to use as specified in the SequenceGenerator 
 or TableGenerator annotation.See JavaDoc Reference Page...="seq")
    @Idjavax.persistence.IdJPA annotationSpecifies the primary key of an entity.See JavaDoc Reference Page... long id;
}

Unlike AUTOGenerationType.AUTOenum constantIndicates that the persistence provider should pick an appropriate strategy for the particular database.See JavaDoc Reference Page... and IDENTITYGenerationType.IDENTITYenum constantIndicates that the persistence provider must assign primary keys for the entity using a database identity column.See JavaDoc Reference Page..., the SEQUENCEGenerationType.SEQUENCEenum constantIndicates that the persistence provider must assign primary keys for the entity using a database sequence.See JavaDoc Reference Page... 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 allocationSizeSequenceGenerator.allocationSizeannotation element(Optional) The amount to increment by when allocating sequence numbers from the sequence.See JavaDoc Reference Page... 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 TABLEGenerationType.TABLEenum constantIndicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.See JavaDoc Reference Page... strategy is very similar to the SEQUENCEGenerationType.SEQUENCEenum constantIndicates that the persistence provider must assign primary keys for the entity using a database sequence.See JavaDoc Reference Page... strategy:

@Entityjavax.persistence.EntityJPA annotationSpecifies that the class is an entity.See JavaDoc Reference Page...
@TableGeneratorjavax.persistence.TableGeneratorJPA annotationDefines a primary key generator that may be 
 referenced by name when a generator element is specified for 
 the GeneratedValue annotation.See JavaDoc Reference Page...(nameTableGenerator.nameannotation element(Required) A unique generator name that can be referenced 
 by one or more classes to be the generator for id values.See JavaDoc Reference Page...="tab", initialValueTableGenerator.initialValueannotation element(Optional) The initial value to be used to initialize the column
 that stores the last value generated.See JavaDoc Reference Page...=0, allocationSizeTableGenerator.allocationSizeannotation element(Optional) The amount to increment by when allocating id 
 numbers from the generator.See JavaDoc Reference Page...=50)
public class EntityWithTableId {
    @GeneratedValuejavax.persistence.GeneratedValueJPA annotationProvides for the specification of generation strategies for the
 values of primary keys.See JavaDoc Reference Page...(strategyGeneratedValue.strategyannotation element(Optional) The primary key generation strategy
 that the persistence provider must use to
 generate the annotated entity primary key.See JavaDoc Reference Page...=GenerationTypejavax.persistence.GenerationTypeJPA enumDefines the types of primary key generation strategies.See JavaDoc Reference Page....TABLEGenerationType.TABLEenum constantIndicates that the persistence provider must assign 
 primary keys for the entity using an underlying 
 database table to ensure uniqueness.See JavaDoc Reference Page..., generatorGeneratedValue.generatorannotation element(Optional) The name of the primary key generator
 to use as specified in the SequenceGenerator 
 or TableGenerator annotation.See JavaDoc Reference Page...="tab")
    @Idjavax.persistence.IdJPA annotationSpecifies the primary key of an entity.See JavaDoc Reference Page... 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 TABLEGenerationType.TABLEenum constantIndicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.See JavaDoc Reference Page... and SEQUENCEGenerationType.SEQUENCEenum constantIndicates that the persistence provider must assign primary keys for the entity using a database sequence.See JavaDoc Reference Page... strategies are almost identical.

A tiny difference is related to the initial value attribute. Whereas the SEQUENCEGenerationType.SEQUENCEenum constantIndicates that the persistence provider must assign primary keys for the entity using a database sequence.See JavaDoc Reference Page... strategy maintains the next sequence number to be used the TABLEGenerationType.TABLEenum constantIndicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.See JavaDoc Reference Page... 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 TABLEGenerationType.TABLEenum constantIndicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.See JavaDoc Reference Page... strategy initialValue=0 has to be specified in the @SequenceGeneratorjavax.persistence.SequenceGeneratorJPA annotationDefines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation.See JavaDoc Reference Page... annotation.