@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqGen")
@SequenceGenerator(name = "seqGen", sequenceName = "seq", initialValue = 1)
private Long id;
Basically, i have three entities (fetched at the same times) that share the same sequenceName in my database. I know for performance purpose, it's better to use the same tab for every entities with a SequenceGenerator.
When fetching a lot of datas, sometimes, i have a duplicate entries error, and basically changing the next_val of my sequence fix the problem temporarly. I want to know how the SequenceGenerator work better and if there is a work around this issue ?
EDIT : could you explain me better what exactly is the purpose of a sequence and how his algorithm works ?
EDIT 2 : in a post, i saw this comment :
"Assume that you create a sequence object that has the CACHE option enabled in Microsoft SQL Server 2012 or SQL Server 2014. When the instance is under memory pressure, and multiple concurrent connections request sequence values from the same sequence object, duplicate sequence values may be generated. In addition, a unique or primary key (PK) violation error occurs when the duplicate sequence value is inserted into a table."
Is there a link with my current issue ?
There is no algorithm for sequence, it is +1 similar to autoincrement, also it has nothing to do with fetching your data that has duplicate entries. Duplicate entries only mean that your database or code has a bad structure which leads to allowing data to be duplicated in the first place.
GenerationType. IDENTITY − In identity, the database is responsible to auto-generate the primary key. Insert a row without specifying a value for the ID and after inserting the row, ask the database for the last generated ID. Oracle 11g does not support an identity key generator. This feature is supported in Oracle 12c.
GenerationType. SEQUENCE − In sequence, we first ask the database for the next set of the sequence then we insert a row with return sequence id.
https://www.baeldung.com/hibernate-identifiers