0
0
Spring Bootframework~10 mins

@ManyToMany 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-many relationship in a Spring Boot entity.

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

@Entity
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @[1]
    private Set<Course> courses = new HashSet<>();

    // getters and setters
}
Drag options to blanks, or click blank then click option'
AOneToMany
BOneToOne
CManyToMany
DManyToOne
Attempts:
3 left
💡 Hint
Common Mistakes
Using @OneToMany instead of @ManyToMany
Forgetting to import the correct annotation
Using @ManyToOne which is for many-to-one relationships
2fill in blank
medium

Complete the code to specify the join table for the many-to-many relationship.

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

@Entity
public class Student {

    @ManyToMany
    @[1](
        name = "student_course",
        joinColumns = @JoinColumn(name = "student_id"),
        inverseJoinColumns = @JoinColumn(name = "course_id")
    )
    private Set<Course> courses = new HashSet<>();

    // getters and setters
}
Drag options to blanks, or click blank then click option'
AJoinTable
BEntity
CJoinColumn
DTable
Attempts:
3 left
💡 Hint
Common Mistakes
Using @JoinColumn instead of @JoinTable for the join table
Using @Table which is for entity tables, not join tables
3fill in blank
hard

Fix the error in the code to correctly map the many-to-many relationship inverse side.

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

@Entity
public class Course {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;

    @ManyToMany(mappedBy = "[1]")
    private Set<Student> students = new HashSet<>();

    // getters and setters
}
Drag options to blanks, or click blank then click option'
Acourses
Bstudents
Ctitle
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'students' instead of 'courses' in mappedBy
Using a field name that does not exist
4fill in blank
hard

Fill both blanks to complete the code for the join table with cascade and fetch type.

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

@Entity
public class Student {

    @ManyToMany(cascade = CascadeType.[1], fetch = FetchType.[2])
    @JoinTable(
        name = "student_course",
        joinColumns = @JoinColumn(name = "student_id"),
        inverseJoinColumns = @JoinColumn(name = "course_id")
    )
    private Set<Course> courses = new HashSet<>();

    // getters and setters
}
Drag options to blanks, or click blank then click option'
AALL
BEAGER
CLAZY
DREMOVE
Attempts:
3 left
💡 Hint
Common Mistakes
Using FetchType.EAGER which loads immediately and can cause performance issues
Using CascadeType.REMOVE only instead of ALL
5fill in blank
hard

Fill all three blanks to complete the bidirectional many-to-many relationship with proper annotations and collection types.

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

@Entity
public class Course {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;

    @ManyToMany(mappedBy = "[1]", cascade = CascadeType.[2], fetch = FetchType.[3])
    private Set<Student> students = new HashSet<>();

    // getters and setters
}
Drag options to blanks, or click blank then click option'
Acourses
BALL
CLAZY
DList
Attempts:
3 left
💡 Hint
Common Mistakes
Using List instead of Set for many-to-many collections
Incorrect mappedBy value
Using FetchType.EAGER unnecessarily