@generatedvalue(strategy = generationtype.identity) code example
Example 1: meaning of generationtype.auto
Simply, @Id: This annotation specifies the primary key of the entity.
@GeneratedValue: This annotation is used to specify the primary key generation strategy to use. i.e Instructs database to generate a value for this field automatically. If the strategy is not specified by default AUTO will be used.
GenerationType enum defines four strategies:
1. Generation Type . TABLE,
2. Generation Type. SEQUENCE,
3. Generation Type. IDENTITY
4. Generation Type. AUTO
GenerationType.SEQUENCE
With this strategy, underlying persistence provider must use a database sequence to get the next unique primary key for the entities.
GenerationType.TABLE
With this strategy, underlying persistence provider must use a database table to generate/keep the next unique primary key for the entities.
GenerationType.IDENTITY
This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence. If underlying database doesn't support IDENTITY column or some similar variant then the persistence provider can choose an alternative appropriate strategy. In this examples we are using H2 database which doesn't support IDENTITY column.
GenerationType.AUTO
This GenerationType indicates that the persistence provider should automatically pick an appropriate strategy for the particular database. This is the default GenerationType, i.e. if we just use @GeneratedValue annotation then this value of GenerationType will be used.
Example 2: id auto generated jpa
@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;
:
}