ViliDuarte1
ViliDuarte1

Reputation: 15

How force ID generator for class "indentity" to increment for last inserted Id in SQL

I want ask for some advice about,why my <generator class="identity" /> generate id in table not as as last inserted + 1 but like e.g last insert ID is 5, and when save record then a new Id has be for example 7, so ID value 6 is skipped

(i need use identity not increment class due to advantage of this "identity" class for my mySql database)

because in my case i need same ID value for primary key and foreign key, must be equals,,but in this case i got Primary key 7 and Foreign key will generate also but not as incremented by last ID,

both class has Hibernate mappings, with this <generator class="identity" /> What i must use so that to obtain both incremented ID with new record by "identity"?

Upvotes: 0

Views: 258

Answers (1)

Olivier Catteau
Olivier Catteau

Reputation: 64

You have to create your table with a primary key using "AUTO_INCREMENT" such as :

CREATE TABLE person ( id BIGINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (id) )

Then your GenerationType.IDENTITY must do the job.

Upvotes: 2

Related Questions