0
0
Spring Bootframework~5 mins

@Id and @GeneratedValue for primary keys in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Id annotation in Spring Boot?
The @Id annotation marks a field as the primary key of an entity. It tells Spring Boot which field uniquely identifies each record in the database.
Click to reveal answer
beginner
What does the @GeneratedValue annotation do?
The @GeneratedValue annotation tells Spring Boot to automatically generate a value for the primary key when saving a new entity. It helps avoid manually setting unique IDs.
Click to reveal answer
intermediate
Which strategy options can you use with @GeneratedValue?
Common strategies include AUTO (default, lets the provider choose), IDENTITY (database auto-increment), SEQUENCE (uses a database sequence), and TABLE (uses a table to generate IDs).
Click to reveal answer
beginner
Why is it important to use @Id and @GeneratedValue together?
Using @Id identifies the primary key field, and @GeneratedValue automates its value creation. Together, they ensure each entity has a unique, automatically assigned ID.
Click to reveal answer
beginner
Show a simple example of an entity with @Id and @GeneratedValue annotations.
Example:
<pre>
@Entity
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String name;
  // getters and setters
}</pre>
Click to reveal answer
What does the @Id annotation specify in a Spring Boot entity?
AThe primary key field
BA foreign key field
CA regular column
DA database table name
Which annotation automatically generates the primary key value?
A@GeneratedValue
B@Column
C@Entity
D@Table
What is the default strategy for @GeneratedValue if none is specified?
AIDENTITY
BAUTO
CSEQUENCE
DTABLE
Which strategy uses a database sequence to generate IDs?
AIDENTITY
BAUTO
CSEQUENCE
DTABLE
Why should you not manually assign values to a field annotated with @GeneratedValue?
ABecause the field is read-only
BBecause the database will ignore manual values
CBecause it will cause duplicate keys
DBecause the value is automatically generated and manual assignment can cause errors
Explain how @Id and @GeneratedValue work together in a Spring Boot entity.
Think about how the database knows which field is the unique identifier and how it gets its value.
You got /4 concepts.
    Describe the different strategies available for @GeneratedValue and when you might use each.
    Consider how different databases handle ID generation.
    You got /5 concepts.