0
0
Spring Bootframework~10 mins

@ManyToOne relationship 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 declare a many-to-one relationship in a Spring Boot entity.

Spring Boot
import jakarta.persistence.*;

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

    @[1]
    private Customer customer;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
AManyToMany
BManyToOne
COneToMany
DOneToOne
Attempts:
3 left
💡 Hint
Common Mistakes
Using @OneToMany instead of @ManyToOne
Forgetting to import the annotation
Confusing the direction of the relationship
2fill in blank
medium

Complete the code to specify the foreign key column name for the many-to-one relationship.

Spring Boot
import jakarta.persistence.*;

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

    @ManyToOne
    @JoinColumn(name = "[1]")
    private Customer customer;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
Aorder_id
BcustomerName
Cid
Dcustomer_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong column name that doesn't exist
Confusing the primary key column with the foreign key column
3fill in blank
hard

Fix the error in the code to correctly map the many-to-one relationship with lazy loading.

Spring Boot
import jakarta.persistence.*;

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

    @ManyToOne(fetch = FetchType.[1])
    @JoinColumn(name = "customer_id")
    private Customer customer;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
ALAZY
BIMMEDIATE
CDEFERRED
DEAGER
Attempts:
3 left
💡 Hint
Common Mistakes
Using EAGER when lazy loading is intended
Using invalid fetch type values
4fill in blank
hard

Fill both blanks to complete the code for a bidirectional many-to-one relationship.

Spring Boot
import jakarta.persistence.*;
import java.util.List;

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

    @[1](mappedBy = "customer")
    private List<Order> [2];

    // getters and setters
}
Drag options to blanks, or click blank then click option'
AOneToMany
BManyToOne
Corders
DcustomerOrders
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ManyToOne on the 'one' side
Using a singular field name for a list
5fill in blank
hard

Fill all three blanks to complete the code for an Order entity with a many-to-one relationship and cascade persist.

Spring Boot
import jakarta.persistence.*;

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

    @ManyToOne(cascade = CascadeType.[1], fetch = FetchType.[2])
    @JoinColumn(name = "[3]")
    private Customer customer;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
APERSIST
BLAZY
Ccustomer_id
DREMOVE
Attempts:
3 left
💡 Hint
Common Mistakes
Using REMOVE cascade when not intended
Using EAGER fetch when lazy is better for performance
Wrong join column name