Resetting auto-generated id for MS SQL

Hello, I am using Hibernate 5.4.25.Final to perform CRUD operations in Oracle, Postgres and MS SQL DB’s. In a table where the auto generated key is an “int”, after a certain number of days, the “id” reaches the max value of (2^31 -1) and no moere records can be written until “DBCC CHECKIDENT (‘dbo.tbl’,RESEED,0);” operation is executed manually.

I wanted to find out if there is any way to perform the following operations using hibernate:

  1. Determine the max value (e.g. in this case it will be 2^31-1)
  2. If the auto generated value is equal to this max value, then execute reset id (e.g. execute “DBCC CHECKIDENT (‘dbo.tbl’,RESEED,0);”).

Thanks so much in advance for your help.

You can of course just use EntityManager.createNativeQuery to implement what you need, but I would be worried if the sequence reaches the maximum. Maybe you misconfigured the allocation size or increment size for the sequence? Or are you in fact inserting and removing that many elements in a few days that you cycle through all values?