Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Create a @ManyToMany Relationship in Spring Boot
📖 Scenario: You are building a simple library system where Books can have multiple Authors, and each Author can write multiple Books.This is a real-world example of a many-to-many relationship.
🎯 Goal: Build two Spring Boot entity classes Book and Author with a proper @ManyToMany relationship between them.
📋 What You'll Learn
Create a Book entity with an id and title field
Create an Author entity with an id and name field
Add a @ManyToMany relationship between Book and Author
Use a join table named book_author with join columns book_id and author_id
💡 Why This Matters
🌍 Real World
Many real-world applications like library systems, social networks, and e-commerce platforms use many-to-many relationships to connect entities.
💼 Career
Understanding @ManyToMany relationships is essential for backend developers working with Spring Boot and relational databases.
Progress0 / 4 steps
1
Create the Book entity
Create a Spring Boot entity class called Book with fields: Long id annotated with @Id and @GeneratedValue, and String title. Annotate the class with @Entity.
Spring Boot
Hint
Use @Entity on the class, and add @Id and @GeneratedValue on the id field.
2
Create the Author entity
Create a Spring Boot entity class called Author with fields: Long id annotated with @Id and @GeneratedValue, and String name. Annotate the class with @Entity.
Spring Boot
Hint
Similar to Book, use @Entity and add @Id and @GeneratedValue on the id field.
3
Add @ManyToMany relationship in Book
In the Book class, add a field Set<Author> authors annotated with @ManyToMany and @JoinTable. Use name = "book_author" for the join table, joinColumns = @JoinColumn(name = "book_id"), and inverseJoinColumns = @JoinColumn(name = "author_id"). Initialize authors as a new HashSet<>().
Spring Boot
Hint
Use @ManyToMany and @JoinTable with the specified join table and columns. Initialize the authors set.
4
Add @ManyToMany relationship in Author
In the Author class, add a field Set<Book> books annotated with @ManyToMany(mappedBy = "authors"). Initialize books as a new HashSet<>().
Spring Boot
Hint
Use @ManyToMany(mappedBy = "authors") on the books field and initialize it as a new HashSet<>().
Practice
(1/5)
1. What does the @ManyToMany annotation represent in Spring Boot?
easy
A. A relationship where entities inherit from each other.
B. A relationship where one entity has only one of the other entity.
C. A relationship where entities are unrelated.
D. A relationship where each entity can have many of the other entity.
Solution
Step 1: Understand the meaning of @ManyToMany
The annotation defines a link where each entity can be related to many instances of the other entity.
Step 2: Compare with other relationship types
Unlike one-to-one or one-to-many, many-to-many allows multiple connections on both sides.
Final Answer:
A relationship where each entity can have many of the other entity. -> Option D
Quick Check:
@ManyToMany = many-to-many link [OK]
Hint: Think: many items linked to many others [OK]
Common Mistakes:
Confusing with one-to-one or one-to-many
Thinking it means inheritance
Ignoring the bidirectional nature
2. Which of the following is the correct way to define a join table in a @ManyToMany relationship?
Hint: joinColumns = this entity, inverseJoinColumns = other entity [OK]
Common Mistakes:
Swapping joinColumns and inverseJoinColumns
Using @JoinColumn instead of @JoinTable
Omitting joinColumns or inverseJoinColumns
3. Given the following code snippet, what will be the output when printing student.getCourses().size() after adding two courses to the student?
@Entity
class Student {
@ManyToMany
Set<Course> courses = new HashSet<>();
public Set<Course> getCourses() { return courses; }
}
@Entity
class Course {}
Student student = new Student();
Course c1 = new Course();
Course c2 = new Course();
student.getCourses().add(c1);
student.getCourses().add(c2);
System.out.println(student.getCourses().size());
medium
A. 2
B. 0
C. 1
D. Compilation error
Solution
Step 1: Understand the collection type and additions
The courses field is a HashSet, which allows unique elements. Adding two different Course objects increases size to 2.
Step 2: Confirm no errors in adding elements
Adding elements to the set is valid and no exceptions occur here.
Final Answer:
2 -> Option A
Quick Check:
Two courses added = size 2 [OK]
Hint: HashSet size equals unique added elements [OK]
Common Mistakes:
Assuming size is 0 because of missing persistence
Confusing with list allowing duplicates
Expecting compilation error due to missing annotations
4. Identify the error in this @ManyToMany mapping:
@Entity
class Author {
@ManyToMany
Set<Book> books;
}
@Entity
class Book {
@ManyToMany(mappedBy = "books")
Set<Author> authors;
}
medium
A. The @ManyToMany annotation is missing @JoinTable on both sides.
B. The mappedBy attribute is incorrectly used on the owning side.
C. The collections are not initialized, causing NullPointerException.
D. The entities must extend a common superclass.
Solution
Step 1: Check collection initialization
The sets 'books' and 'authors' are declared but not initialized, so they are null by default.
Step 2: Understand impact of null collections
Trying to add or access elements will cause NullPointerException at runtime.
Final Answer:
The collections are not initialized, causing NullPointerException. -> Option C
Quick Check:
Uninitialized sets cause null errors [OK]
Hint: Always initialize collections to avoid null errors [OK]
Common Mistakes:
Assuming @JoinTable is mandatory on both sides
Confusing owning side with inverse side
Thinking inheritance is required
5. You have two entities, Student and Club, with a @ManyToMany relationship. You want to add a new club to a student and ensure both sides reflect this change. Which code snippet correctly updates both sides?
hard
A. student.getClubs().add(club); club.getStudents().add(student);
Both sides must be updated to keep the relationship consistent in memory.
Step 2: Check which code updates both sides
student.getClubs().add(club); club.getStudents().add(student); adds the club to the student's clubs and the student to the club's students, syncing both sides.
Final Answer:
student.getClubs().add(club); club.getStudents().add(student); -> Option A
Quick Check:
Update both sides for consistency [OK]
Hint: Always update both sides of @ManyToMany [OK]