1

When persisting data with JPA and sequence allocation size of "20", the DB is queried for the next 20 ids to be used, so the entities stored have the id values 1, 2, 3, ..

But if doing a direct SQL INSERT on the (h2) DB, the ids are really incremented by "20", resulting in id values: 21, 41, 61, ...

Is there a way to keep the JPA handling, while the SQL INSERT increase the ids also only be "1", resulting in values: 21, 22, 23, ... ?

Java JPA Entity

@Id
@SequenceGenerator( name="team_gen", sequenceName="team_seq", initialValue=1, allocationSize=20 )
@GeneratedValue( strategy=GenerationType.SEQUENCE, generator="team_gen" )
private Long id;

SQL

CREATE SEQUENCE team_seq START WITH 1 INCREMENT BY 20;
CREATE TABLE IF NOT EXISTS team (
    id BIGINT DEFAULT team_seq.nextval PRIMARY KEY,
    team_name VARCHAR(255) NOT NULL
);

I'm using Spring Boot with H2 and Flyway for DB migration through SQL.


Edit and explanation why it's not a duplicate: The other question focuses on the insertion order all done with hibernate while my questions asks for a way to:

  • keep JPA handling with allocationSize=20 to reserve the next 20 PK ids with a single DB request, then persisting rows with increment steps of 1
  • persists per direct sql insert should also keep increment steps of 1 (but actually they have an increment step of "20" due to the "INCREMENT BY 20" sequence definition.
Krabat
  • 133
  • 1
  • 6
  • Possible duplicate of [Hibernate, @SequenceGenerator and allocationSize](https://stackoverflow.com/questions/12745751/hibernate-sequencegenerator-and-allocationsize) – Patrick W Mar 27 '19 at 13:42
  • Sorry, but my question is not a duplicate. I read the other thread before and sadly it doesn't help me with my issue. – Krabat Mar 27 '19 at 14:03

0 Answers0