Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
✗ Incorrect
The @Id annotation marks the field as the primary key of the entity.
Which annotation automatically generates the primary key value?
A@GeneratedValue
B@Column
C@Entity
D@Table
✗ Incorrect
@GeneratedValue tells Spring Boot to generate the primary key value automatically.
What is the default strategy for @GeneratedValue if none is specified?
AIDENTITY
BAUTO
CSEQUENCE
DTABLE
✗ Incorrect
AUTO lets the persistence provider choose the best strategy automatically.
Which strategy uses a database sequence to generate IDs?
AIDENTITY
BAUTO
CSEQUENCE
DTABLE
✗ Incorrect
SEQUENCE uses a database sequence object to generate unique IDs.
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
✗ Incorrect
Manual assignment can conflict with automatic generation and 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.
Practice
(1/5)
1. What is the main purpose of the @Id annotation in a Spring Boot entity?
easy
A. To specify a foreign key relationship
B. To generate unique values automatically
C. To define a database table name
D. To mark the primary key field of the entity
Solution
Step 1: Understand the role of @Id
The @Id annotation marks a field as the primary key in a database entity.
Step 2: Differentiate from other annotations
@GeneratedValue generates values, but @Id specifically identifies the primary key field.
Final Answer:
To mark the primary key field of the entity -> Option D
Quick Check:
@Id marks primary key [OK]
Hint: Remember: @Id means 'this is the primary key' [OK]
Common Mistakes:
Confusing @Id with @GeneratedValue
Thinking @Id generates values automatically
Using @Id to name tables
2. Which of the following is the correct way to use @GeneratedValue with GenerationType.IDENTITY in a Spring Boot entity?
easy
A. @GeneratedValue(strategy = GenerationType.AUTO)
B. @GeneratedValue(strategy = GenerationType.SEQUENCE)
C. @GeneratedValue(strategy = GenerationType.IDENTITY)
D. @GeneratedValue(strategy = GenerationType.TABLE)
Solution
Step 1: Identify the correct strategy for identity generation
GenerationType.IDENTITY is used to let the database auto-increment the primary key.
Step 2: Match the annotation syntax
The correct syntax is @GeneratedValue(strategy = GenerationType.IDENTITY).
Final Answer:
@GeneratedValue(strategy = GenerationType.IDENTITY) -> Option C
Quick Check:
Use GenerationType.IDENTITY with strategy = GenerationType.IDENTITY [OK]
Hint: Use strategy = GenerationType.IDENTITY for auto-increment keys [OK]
Common Mistakes:
Using AUTO instead of IDENTITY for auto-increment
Omitting the strategy parameter
Confusing SEQUENCE with IDENTITY
3. Given the entity code below, what will be the value of user.getId() after saving a new user to the database?
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// getters and setters
}
medium
A. A unique auto-generated Long value
B. null
C. The name of the user
D. An exception will be thrown
Solution
Step 1: Understand @GeneratedValue with IDENTITY
This strategy lets the database generate a unique primary key value automatically when saving.
Step 2: Predict the value after saving
After saving, user.getId() will hold the generated unique Long value assigned by the database.
Final Answer:
A unique auto-generated Long value -> Option A
Quick Check:
@GeneratedValue with IDENTITY creates unique IDs [OK]
Hint: After save, IDENTITY generates a unique Long ID automatically [OK]
Common Mistakes:
Expecting id to be null after save
Confusing id with other fields
Assuming an error occurs without database setup
4. Consider this entity code snippet:
@Entity
public class Product {
@Id
@GeneratedValue
private Long productId;
private String name;
}
What is the likely problem with this code?
medium
A. Missing strategy in @GeneratedValue may cause unexpected ID generation
B. The field productId should not be private
C. The @Id annotation is missing
D. The entity class must implement Serializable
Solution
Step 1: Check @GeneratedValue usage
The @GeneratedValue annotation without specifying a strategy defaults to AUTO, which may behave differently depending on the database.
Step 2: Understand impact of missing strategy
This can cause unexpected ID generation behavior if the database does not support the default strategy well.
Final Answer:
Missing strategy in @GeneratedValue may cause unexpected ID generation -> Option A
Quick Check:
Always specify strategy to avoid surprises [OK]
Hint: Always specify strategy in @GeneratedValue to avoid surprises [OK]
Common Mistakes:
Thinking private fields cause errors
Believing @Id is missing
Assuming Serializable is mandatory
5. You want to create a Spring Boot entity with a primary key that uses a database sequence named user_seq. Which is the correct way to annotate the ID field?
hard
A. @Id @GeneratedValue(strategy = GenerationType.IDENTITY)