@GeneratedValue: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[JPA Concepts#@GeneratedValue|JPA Concepts]]
* [[JPA Concepts#@GeneratedValue|JPA Concepts]]
* [[Spring_Data_JPA#Annotate_Domain_Objects_with_.40Entity|Spring Data JPA]]
* [[Spring_Data_JPA#Define_Entity.27s_ID|Spring Data JPA]]


=Overview=
=Overview=
Line 8: Line 8:
<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.AUTO)
</syntaxhighlight>
Used together with [[@Id]] when relying on the database to automatically generate the ID value, define "strategy = AUTO". This declaration works with an [[H2]] database.
=Cases=
For a PostgreSQL table in which the primary key was declared as follows:
<syntaxhighlight lang='sql'>
id BIGINT UNIQUE GENERATED ALWAYS AS IDENTITY,
</syntaxhighlight>
the corresponding annotation combination that works is:
<syntaxhighlight lang='java'>
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 20:07, 10 November 2018

Internal

Overview

@GeneratedValue(strategy = GenerationType.AUTO)

Used together with @Id when relying on the database to automatically generate the ID value, define "strategy = AUTO". This declaration works with an H2 database.

Cases

For a PostgreSQL table in which the primary key was declared as follows:

id BIGINT UNIQUE GENERATED ALWAYS AS IDENTITY,

the corresponding annotation combination that works is:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;