Issue
I have the artistId set in the following code:
@Id
@SequenceGenerator(
name = "artist_sequence",
sequenceName = "artist_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "artist_sequence"
)
private Long artistId;
Can someone explain to me why my artistId sometimes skips values and is out of order? What I noticed is that even if I try to insert data into the table that is invalid (when an exception is thrown), that data is also rejected by the table entry, but my artistId will be filled invisibly. This brings me to the following state in my table:
artist_id | first_name | last_name |
---|---|---|
1 | Marshall | Mathers |
3 | Tupac | Shakur |
As I said, my artistId with value 2 is skipped because I tried to insert the artist that already exists, but I set up that it must be unique. So, the exception is thrown, data is rejected, but my id with value 2 is filled somehow(or it is skipped). Can you guys help me, how can I solve this problem to avoid this?
Solution
This is pretty much normal behavior.
The sole responsibility of the sequence generator is to produce distinct integer values, nothing more.
Answered By - mhrsalehi
Answer Checked By - Katrina (JavaFixing Volunteer)