0
0
Spring Bootframework~10 mins

@Id and @GeneratedValue for primary keys in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Id and @GeneratedValue for primary keys
Define Entity Class
Add @Id Annotation
Add @GeneratedValue Annotation
Save Entity Instance
Database Auto-generates Primary Key
Entity Saved with Unique ID
This flow shows how marking a field with @Id and @GeneratedValue lets Spring Boot auto-generate unique primary keys when saving entities.
Execution Sample
Spring Boot
import jakarta.persistence.*;

@Entity
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
}
Defines a User entity with an auto-generated primary key 'id'.
Execution Table
StepActionEntity Field 'id' ValueDatabase Primary Key GenerationResult
1Create new User instancenullNo DB action yetUser object created with id=null
2Call save(user)1DB generates new id=1User saved with id=1
3Create another User instancenullNo DB action yetSecond User object created with id=null
4Call save(secondUser)2DB generates new id=2Second User saved with id=2
5Retrieve first User1No DB actionUser with id=1 retrieved
6Retrieve second User2No DB actionUser with id=2 retrieved
💡 Execution stops after saving and retrieving entities with auto-generated primary keys.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
user.idnull111
secondUser.idnullnull22
Key Moments - 3 Insights
Why is the 'id' field null before saving the entity?
Because the database generates the primary key only when the entity is saved, as shown in execution_table step 2.
What does @GeneratedValue(strategy = GenerationType.IDENTITY) do?
It tells Spring Boot to let the database auto-generate a unique primary key when saving, as seen in the DB generating ids 1 and 2 in steps 2 and 4.
Can we manually set the 'id' value when using @GeneratedValue?
Usually no, because the database controls the id generation to avoid duplicates, so the id is null before saving and assigned after.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'user.id' after step 2?
A0
Bnull
C1
Dundefined
💡 Hint
Check the 'Entity Field id Value' column at step 2 in the execution_table.
At which step does the database generate the primary key for the second user?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Database Primary Key Generation' column for the second user in the execution_table.
If we remove @GeneratedValue, what would happen to the 'id' field before saving?
AIt would still be auto-generated by the database
BIt would remain null unless manually set
CIt would cause a compile error
DIt would automatically be set to 0
💡 Hint
Think about who assigns the id when @GeneratedValue is missing, referencing variable_tracker.
Concept Snapshot
@Id marks the primary key field in an entity.
@GeneratedValue tells Spring Boot to auto-generate this key.
Common strategy: GenerationType.IDENTITY uses DB auto-increment.
Before saving, id is null; after saving, id is assigned.
This ensures unique IDs without manual input.
Full Transcript
In Spring Boot, the @Id annotation marks a field as the primary key of an entity. The @GeneratedValue annotation instructs the framework to let the database generate a unique primary key automatically when the entity is saved. Typically, the GenerationType.IDENTITY strategy is used, which relies on the database's auto-increment feature. When a new entity instance is created, its id field is null. Upon saving, the database assigns a unique id, which is then set back into the entity. This process avoids manual id management and ensures unique primary keys for each saved entity.