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?
✗ Incorrect
The @Id annotation marks the field as the primary key of the entity.
Which annotation automatically generates the primary key value?
✗ Incorrect
@GeneratedValue tells Spring Boot to generate the primary key value automatically.
What is the default strategy for @GeneratedValue if none is specified?
✗ Incorrect
AUTO lets the persistence provider choose the best strategy automatically.
Which strategy uses a database sequence to generate IDs?
✗ Incorrect
SEQUENCE uses a database sequence object to generate unique IDs.
Why should you not manually assign values to a field annotated with @GeneratedValue?
✗ 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.