The Many-To-Many mapping represents a collection-valued association where any number of entities can be associated with a collection of other entities. In relational database any number of rows of one entity can be referred to any number of rows of another entity.
@ManyToMany Example
In this example, we will create a Many-To-Many relationship between a Student and Library in such a way that any number of students can be issued any type of books.
This example contains the following steps: -
Step 1. Create an entity class Student.java under com.javahubpoint.mapping package that contains student id (s_id) and student name (s_name) with @ManyToMany annotation that contains Library class object of List type.
Student.java
import javax.persistence.*;
@Entity
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int s_id;
private String s_name;
@ManyToMany(targetEntity=Library.class)
private List lib;
public Student(int s_id, String s_name, List lib) {
super();
this.s_id = s_id;
this.s_name = s_name;
this.lib = lib;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public List getLib() {
return lib;
}
public void ListLib(List lib) {
this.lib = lib;
}
public int getS_id() {
return s_id;
}
public void ListS_id(int s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void ListS_name(String s_name) {
this.s_name = s_name;
}
}
Step 2. Create another entity class Library.java under com.javahubpoint.mapping package that contains book id (b_id), book name (b_name) with @ManyToMany annotation that contains Student class object of List type.
Library.java
import java.util.List;
import javax.persistence.*;
@Entity
public class Library {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int b_id;
private String b_name;
@ManyToMany(targetEntity=Student.class)
private List stud;
public Library(int b_id, String b_name, List stud) {
super();
this.b_id = b_id;
this.b_name = b_name;
this.stud = stud;
}
public Library() {
super();
// TODO Auto-generated constructor stub
}
public int getB_id() {
return b_id;
}
public void ListB_id(int b_id) {
this.b_id = b_id;
}
public String getB_name() {
return b_name;
}
public void ListB_name(String b_name) {
this.b_name = b_name;
}
public List getStud() {
return stud;
}
public void ListStud(List stud) {
this.stud = stud;
}
}
Step 3. Now, map the entity class and other databases configurations in Persistence.xml file.
Persistence.xml
<persistence-unit name="books_issued">
<class>com.javahubpoint.mapping.Student</class>
<class>com.javahubpoint.mapping.Library</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mapping_db"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="SEVERE"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
</persistence-unit>
</persistence>
Step 4. Create a persistence class ManyToManyExample under com.javahubpoint.ManyToOne package to persist the entity object with data.
package com.javahubpoint.mapping.ManyToMany;
import java.util.ArrayList;
import javax.persistence.*;
import com.javahubpoint.mapping.Student;
import com.javahubpoint.mapping.Library;
public class ManyToManyExample {
public static void main(String[] args) {
EntityManagerFactory emf=Persistence.createEntityManagerFactory("books_issued");
EntityManager em=emf.createEntityManager();
em.getTransaction().begin();
Student st1=new Student(1,"Vipul",null);
Student st2=new Student(2,"Vimal",null);
em.persist(st1);
em.persist(st2);
ArrayList<Student> al1=new ArrayList<Student>();
ArrayList<Student> al2=new ArrayList<Student>();
al1.add(st1);
al1.add(st2);
al2.add(st1);
al2.add(st2);
Library lib1=new Library(101,"Data Structure",al1);
Library lib2=new Library(102,"DBMS",al2);
em.persist(lib1);
em.persist(lib2);
em.getTransaction().commit();
em.close();
emf.close();
}
}
Output:
After the execution of the program, three tables are generated under MySQL workbench.
Student table - This table contains the student details. To fetch data, run select * from student query in MySQL.
Library table - This table contains the library details. To fetch data, run select * from library query in MySQL.
Library_student - This table contains the library details. To fetch data, run select * from library_student query in MySQL.