SteelReed

Search

Search posts, questions…

JPA & HibernateQuestion 2 / 24

Junior

What are @Entity, @Id and @GeneratedValue for? Should you pick IDENTITY, SEQUENCE, UUID or AUTO?

Short answer

@Entity maps a class to a table, @Id marks the primary key, @GeneratedValue picks how ids are made. MySQL uses IDENTITY, PostgreSQL uses SEQUENCE; never leave AUTO on MySQL.

MySQLPostgreSQL, Oraclemade in the appWhich database?IDENTITYAUTO_INCREMENTSEQUENCEallocationSize = 50UUID

3 points to say:

  1. IDENTITY is simple but loses batch inserts.
  2. SEQUENCE grabs 50 ids at once, the fastest.
  3. AUTO on MySQL fakes a sequence with a table, slow.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.