The One-To-Many mapping comes into the category of collection-valued association where an entity is associated with a collection of other entities. Hence, in this type of association the instance of one entity can be mapped with any number of instances of another entity.
@OneToMany Example
In this example, we will create a One-To-Many relationship between a Student and Library in such a way that one student can be issued more than one type of book.
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), student name (s_name) with @OneToMany annotation that contains Library class object of List type.
Student.java
import java.util.List;
import javax.persistence.*;
@Entity
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int s_id;
private String s_name;
@OneToMany(targetEntity=Library.class)
private List books_issued;
public List getBooks_issued() {
return books_issued;
}
public void setBooks_issued(List books_issued) {
this.books_issued = books_issued;
}
public int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_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).
Library.java
import javax.persistence.*;
@Entity
public class Library {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int b_id;
private String b_name;
public Library(int b_id, String b_name) {
super();
this.b_id = b_id;
this.b_name = b_name;
}
public Library() {
super();
// TODO Auto-generated constructor stub
}
public int getB_id() {
return b_id;
}
public void setB_id(int b_id) {
this.b_id = b_id;
}
public String getB_name() {
return b_name;
}
public void setB_name(String b_name) {
this.b_name = b_name;
}
}
Step 3. Now, map the entity class and other databases configuration 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 OneToManyExample under com.javahubpoint.OneToOne package to persist the entity object with data.
OneToManyExample.java
import java.util.ArrayList;
import javax.persistence.*;
import com.javahubpoint.mapping.Student;
import com.javahubpoint.mapping.Library;
public class OneToManyExample {
public static void main(String[] args) {
EntityManagerFactory emf=Persistence.createEntityManagerFactory("books_issued");
EntityManager em=emf.createEntityManager();
em.getTransaction().begin();
Library lib1=new Library();
lib1.setB_id(101);
lib1.setB_name("Data Structure");
Library lib2=new Library();
lib2.setB_id(102);
lib2.setB_name("DBMS");
em.persist(lib1);
em.persist(lib2);
ArrayList<Library> list=new ArrayList<Library>();
list.add(lib1);
list.add(lib2);
Student st1=new Student();
st1.setS_id(1);
st1.setS_name("Vipul");
st1.setBooks_issued(list);
em.persist(st1);
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 book details. To fetch data, run select * from library query in MySQL.
Student_library table - This table represents the mapping between student and library table. To fetch data, run select * from student_library query in MySQL.