0
0
Spring Bootframework~10 mins

@Id and @GeneratedValue for primary keys in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mark the primary key field in a Spring Boot entity.

Spring Boot
public class User {
    [1]
    private Long id;
}
Drag options to blanks, or click blank then click option'
A@Id
B@Entity
C@Column
D@Table
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Entity instead of @Id for the primary key.
Forgetting to annotate the primary key field.
Using @Column or @Table which are for other purposes.
2fill in blank
medium

Complete the code to automatically generate the primary key value.

Spring Boot
public class User {
    @Id
    [1]
    private Long id;
}
Drag options to blanks, or click blank then click option'
A@Transient
B@Column(nullable = false)
C@Table(name = "users")
D@GeneratedValue(strategy = GenerationType.AUTO)
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Column instead of @GeneratedValue for key generation.
Omitting the strategy parameter.
Using @Transient which excludes the field from persistence.
3fill in blank
hard

Fix the error in the code to correctly generate the primary key using identity strategy.

Spring Boot
public class Product {
    @Id
    @GeneratedValue(strategy = [1])
    private Long productId;
}
Drag options to blanks, or click blank then click option'
AGenerationType.IDENTITY
BGenerationType.AUTO
CGenerationType.SEQUENCE
DGenerationType.TABLE
Attempts:
3 left
💡 Hint
Common Mistakes
Using AUTO instead of IDENTITY when the database requires identity strategy.
Confusing SEQUENCE with IDENTITY.
Omitting the strategy parameter.
4fill in blank
hard

Fill both blanks to define a primary key with sequence generation in a Spring Boot entity.

Spring Boot
public class Order {
    @Id
    @GeneratedValue(strategy = [1], generator = [2])
    private Long orderId;
}
Drag options to blanks, or click blank then click option'
AGenerationType.SEQUENCE
B"order_seq"
CGenerationType.IDENTITY
D"seq_order"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing IDENTITY with sequence generator name.
Using generator name without quotes.
Using wrong GenerationType for sequence.
5fill in blank
hard

Fill all three blanks to define a primary key with sequence generation and sequence generator annotation.

Spring Boot
public class Invoice {
    @Id
    @SequenceGenerator(name = [1], sequenceName = [2], allocationSize = 1)
    @GeneratedValue(strategy = [3], generator = [1])
    private Long invoiceId;
}
Drag options to blanks, or click blank then click option'
A"invoice_seq"
B"seq_invoice"
CGenerationType.SEQUENCE
DGenerationType.IDENTITY
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for generator and sequence generator.
Using IDENTITY strategy with sequence generator.
Omitting quotes around string names.