Many to Many Relationship
Posted By : Aekta Bhakar | 30-Apr-2018
A many to many relationships means that a relationship between two or more tables in a database when a parent row in one table contains several child rows in the second table. Vice-versa is also true. A database used by a company or an organization can be taken as an example. There are two tables that the database will contain, one is "Employee" and another one is "Department". Each department can have many numbers of employees working on some task. Similarly, an employee can work for multiple departments. This indicates that multiple Employees can be linked to multiple Departments in another table having columns containing details of "Employees" and "Departments." The details may have their unique ID's.
A database used by a school application can be taken as another example. Two of the tables it contains is "Student" and "Subject".In real life, a student will take several subjects simultaneously, while a subject will be studied by several students at a time. This is a many to many relationships.
Let us implement the above example in Spring Boot and take a look at how many too many mapping is achieved.
There will be two entities one named as Subject and the other one will be Student.
Subject.java
@Entity
@Table(name = "SUBJECTS")
public class Subject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "RECORD_ID")
private Integer id;
@Column(name = "NAME")
private String name;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "courses")
private List courses;
}
And the Student entity class will look something like this:
@Entity
@Table(name = "STUDENTS")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "RECORD_ID")
private Integer id;
@Column(name = "NAME")
private String name;
@Column(name = "AGE")
private Integer age;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "STU_COURSE", joinColumns = {
@JoinColumn(name = "STU_ID") }, inverseJoinColumns = {
@JoinColumn(name = "COURSE_ID") })
private List subjects;
}
The annotation @ManyToMany is used to define many to many
The STU_ID and COURSE_ID will contain the primary key of table STUDENTS and SUBJECTS respectively.
Here mappedBy is used to prevent the creation of the third table from both sides of the entities. If we remove this element, then altogether four tables will be created, the fourth table will be created as subjects_students.
Running this code in Spring Boot as Applications.java as a java application we will see that a separate Student and Subject entries in a database and the third table is STU_COURSE with corresponding mapped entries.
Thanks
Keep reading:)
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aekta Bhakar
Aekta is a Java developer with keen interest in learning new technologies and developing board games.