0
0
Spring Bootframework~20 mins

@Id and @GeneratedValue for primary keys in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Primary Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the effect of @Id annotation in a Spring Boot entity?
In a Spring Boot JPA entity, what does the @Id annotation do?
Spring Boot
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class User {
    @Id
    private Long userId;
    private String name;
}
AMarks the field as the primary key of the entity in the database.
BAutomatically generates a unique value for the field when saving.
CSpecifies the database table name for the entity.
DIndicates the field should be ignored by JPA.
Attempts:
2 left
💡 Hint
Think about what uniquely identifies each record in a database table.
📝 Syntax
intermediate
2:00remaining
Which @GeneratedValue strategy generates values using a database identity column?
In Spring Boot JPA, which @GeneratedValue strategy uses the database's identity column to generate primary key values?
Spring Boot
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;

@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}
AGenerationType.IDENTITY
BGenerationType.SEQUENCE
CGenerationType.TABLE
DGenerationType.AUTO
Attempts:
2 left
💡 Hint
This strategy relies on the database to auto-increment the primary key.
🔧 Debug
advanced
2:00remaining
Why does this entity fail to save with a primary key error?
Given the entity below, why might saving it cause a primary key error?
Spring Boot
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;

@Entity
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long orderId;

    private String product;
}
AThe @Id annotation is missing from the primary key field.
BThe GenerationType.SEQUENCE strategy is invalid and causes a syntax error.
CThe database does not have a sequence defined for this entity's primary key generation.
DThe entity class is missing the @Table annotation specifying the table name.
Attempts:
2 left
💡 Hint
Check if the database supports sequences or if one is configured.
state_output
advanced
2:00remaining
What is the value of 'id' after saving this entity with GenerationType.AUTO?
Consider this entity saved to the database. What will be the value of id after saving?
Spring Boot
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;

@Entity
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    public Long getId() { return id; }
}
AThe value must be manually set before saving; otherwise, it throws an error.
BAlways null because GenerationType.AUTO does not generate values.
CZero because the default value of Long is 0 before saving.
DA unique generated value assigned by the persistence provider after saving.
Attempts:
2 left
💡 Hint
GenerationType.AUTO lets the provider pick the best strategy to generate a unique id.
🧠 Conceptual
expert
2:00remaining
Which statement about @GeneratedValue strategies is true?
Select the only true statement about @GeneratedValue strategies in Spring Boot JPA.
AGenerationType.IDENTITY requires a sequence object in the database to generate keys.
BGenerationType.TABLE uses a separate table to generate unique primary keys and works across all databases.
CGenerationType.SEQUENCE is the default strategy and works without any database setup.
DGenerationType.AUTO disables automatic key generation and requires manual id assignment.
Attempts:
2 left
💡 Hint
Think about which strategy uses a table to generate keys and is database independent.