Julien Berthoud
Julien Berthoud

Reputation: 769

Autoincrement with H2

I have a Springboot application that uses Spring Data. For test purposes only, I use an embedded H2 DB and load some initial data through a data.sql file. My entity are annotated with

@Id
@GeneratedValue(strategy = GenerationType.AUTO)

The problem I'm facing: for the generation of the ids (when I persist some objects), the id-values already used in my initial data (in data.sql) are NOT taken into consideration. As a result it comes to conflict, since it can happen that Hibernate try to use an Id already used. A solution would be to change the GenerationType into IDENTITY, but i'm reluctant to do so, since in production i would prefer having GenerationType.AUTO.

What is best practices to handle this?

Upvotes: 4

Views: 7752

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81998

Tim Biegeleisen seemed to already have convinced you: Use IDENTITY which seems to work in both cases.

The alternative I would recommend nowadays is to use Testcontainers with Postgres in the test and drop H2.

Upvotes: 6

Related Questions