I'm looking for clarification on this question: @GeneratedValue(strategy="IDENTITY") vs. @GeneratedValue(strategy="SEQUENCE") (nearly a decade old has anything changed?)
Getting started with learning jpa, the following generation types both seem to auto-increment the primary keys that get generated by 1.
Identity:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Sequence:
@Id
@SequenceGenerator(
name = "my_sequence",
sequenceName = "my_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "my_sequence"
)
private Long id;
I understand that the sequence object created by GenerationType.SEQUENCE
is not tied directly to a table in the way GenerationType.IDENTITY
is. The old question mentions that sequence can be more flexible. Are there any objective pros or cons to choosing one of these strategies over the other? Also anything new considering the age of the question being referenced?