Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 882
Julien Berthoud Avatar asked Oct 22 '25 10:10

Julien Berthoud


1 Answers

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.

like image 88
Jens Schauder Avatar answered Oct 25 '25 13:10

Jens Schauder