Challenge - 5 Problems
ManyToMany Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this @ManyToMany mapping retrieval?
Given two entities
Student and Course with a bidirectional @ManyToMany relationship, what will be printed when fetching a student and listing their courses?Spring Boot
public class Student { @ManyToMany private Set<Course> courses = new HashSet<>(); // getters and setters } public class Course { @ManyToMany(mappedBy = "students") private Set<Student> students = new HashSet<>(); // getters and setters } // In service: Student s = studentRepository.findById(1L).get(); System.out.println(s.getCourses().size());
Attempts:
2 left
💡 Hint
Think about how JPA initializes collections in a @ManyToMany relationship when accessed inside a transaction.
✗ Incorrect
In a properly configured Spring Boot JPA environment, fetching a student entity and accessing its courses collection inside an open session will load the related courses. The size printed is the actual number of courses linked to that student.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a bidirectional @ManyToMany relationship?
Select the correct code snippet that defines a bidirectional @ManyToMany relationship between
Author and Book entities.Attempts:
2 left
💡 Hint
The owning side does not use mappedBy, the inverse side uses mappedBy with the owning side's field name.
✗ Incorrect
In a bidirectional @ManyToMany, one side is the owner without mappedBy, the other side uses mappedBy referencing the owner's field. Option B correctly shows this pattern.
🔧 Debug
advanced2:30remaining
Why does this @ManyToMany relationship cause a duplicate entry error?
Consider this code snippet where adding the same Course twice to a Student causes a database constraint violation. What is the likely cause?
Spring Boot
Student s = studentRepository.findById(1L).get(); Course c = courseRepository.findById(2L).get(); s.getCourses().add(c); s.getCourses().add(c); studentRepository.save(s);
Attempts:
2 left
💡 Hint
Sets rely on equals and hashCode to avoid duplicates.
✗ Incorrect
If equals and hashCode are not overridden in the entity, the Set cannot detect duplicates properly, causing duplicate inserts and constraint violations.
❓ lifecycle
advanced2:30remaining
What happens when you remove an entity from a @ManyToMany collection and save?
Given a Student entity with a @ManyToMany relationship to Course, what happens in the database when you remove a Course from the Student's courses set and save the Student?
Spring Boot
Student s = studentRepository.findById(1L).get(); Course c = courseRepository.findById(2L).get(); s.getCourses().remove(c); studentRepository.save(s);
Attempts:
2 left
💡 Hint
Removing from a collection updates the join table, not the related entity itself.
✗ Incorrect
Removing a Course from the Student's courses set and saving updates the join table to remove the association link. The Course entity itself remains in the database.
🧠 Conceptual
expert3:00remaining
Which statement about @ManyToMany join tables is true?
Choose the correct statement about the join table used in a @ManyToMany relationship in JPA.
Attempts:
2 left
💡 Hint
Think about how JPA manages many-to-many relationships internally.
✗ Incorrect
By default, the join table has a composite primary key made of the foreign keys from both related entities. It does not store extra attributes unless explicitly mapped as an entity.