Captain Kidd
Captain Kidd

Reputation: 1283

How hibernate Auto-Increment works?

As we know, we always use Auto-Increment for primary key.

I wondering how Auto-Increment works for mysql and hibernate

For example, the lastest primary-key value in the table is 10000. How hibernate understand the current value of primary-key is 10000 and the next value should be 10001? How mysql knows what is the next primary-key value for table?

Upvotes: 2

Views: 1006

Answers (1)

Angelina
Angelina

Reputation: 131

I'm assuming that you're using GenerationType.AUTO to let Hibernate determine how to handle id generation. Here's a similar question to yours: Who is responsible of the auto-incrementation of a primary key between MySQL and Hibernate?

So for MYSQL, hibernate defaults to MYSQL's AUTO_INCREMENT. Explanation of AUTO_INCREMENT can be found here: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

As for how Hibernate / MYSQL knows the last generated id, MYSQL has an AUTO_INCREMENT column for each table which the AUTO_INCREMENT id generation strategy is specified, in which the most recently generated Id is stored.

Upvotes: 2

Related Questions