fe3o4
fe3o4

Reputation: 59

hibernate doesn't update id if another program updates the db

Lets say i have a spring + hibernate application which works fine together. One table A is managed by the application(CRUD). And there might be other programs which also insert some data into the table A. So here comes the problem.

For example: hibernate points the next id to 1001(auto_increment) and before it receives any data, i use a jar file to insert 100 records into the mysql db. So the next id to insert should be 1101 at the point. But if now hibernate receives a record and tries to insert, it throws a exception: java.sql.BatchUpdateException: Duplicate entry '1001' for key 'PRIMARY'. Could you please help? Thanks in advance!

ps: in hbm.xml:

<id name="cns_id" column="cns_id" type="long" access="field">
     <generator class="increment" />
</id>

Upvotes: 1

Views: 166

Answers (1)

npinti
npinti

Reputation: 52195

According to the API, if you do this:

<id name="cns_id" column="cns_id" type="long" access="field">
     <generator class="guid" />
</id>

Hibernate should get the ID from the database:

guid

uses a database-generated GUID string on MS SQL Server and MySQL.

Upvotes: 1

Related Questions