Many-To-One Mapping

The Many-To-One mapping represents a single-valued association where a collection of entities can be associated with the similar entity. Hence, in relational database any more than one row of an entity can refer to the similar rows of another entity.

@ManyToOne Example

In this example, we will create a Many-To-One relationship between a Student and Library in such a way that more than one student can issued the same 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) and student name (s_name) with @ManyToOne annotation that contains an object of Library type.

Student.java

package com.javahubpoint.mapping;
import javax.persistence.*;  
  
@Entity  
public class Student {  
  
    @Id  
    @GeneratedValue(strategy=GenerationType.AUTO)  
    private int s_id;  
    private String s_name;  
      
    @ManyToOne  
    private Library lib;  
      
    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;  
    }  
    public Library getLib() {  
        return lib;  
    }  
    public void setLib(Library lib) {  
        this.lib = lib;  
    }
}  

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

package com.javahubpoint.mapping;
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>  
<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 ManyToOneExample under com.javahubpoint.ManyToOne package to persist the entity object with data.

package com.javahubpoint.mapping.ManyToOne;  
  
import javax.persistence.*;  
import com.javahubpoint.mapping.Student;
import javax.persistence.EntityManagerFactory;
import com.javahubpoint.mapping.Library;  

public class ManyToOneExample {  
  
    public static void main(String[] args) {  
          
        EntityManagerFactory emf=Persistence.createEntityManagerFactory("books_issued");
        EntityManager em=emf.createEntityManager();  
          
        em.getTransaction().begin();  
          
        Library lib=new Library();  
        lib.setB_id(101);  
        lib.setB_name("Data Structure");  
          
        em.persist(lib);  
          
        Student st1=new Student();  
        st1.setS_id(1);  
        st1.setS_name("Vipul");  
       st1.setLib(lib);  
         
        Student st2=new Student();  
        st2.setS_id(2);  
        st2.setS_name("Vimal");  
        st2.setLib(lib);  
          
        em.persist(st1);  
        em.persist(st2); 
          
        em.getTransaction().commit();  
        em.close();  
        emf.close();  
          
    }  
}  

Output:

After the execution of the program, the two tables are generated under MySQL workbench.

Library table - This table contains the library details. To fetch data, run select * from library query in MySQL.




Student table - This table represents the mapping between student and library. To fetch data, run select * from student query in MySQL.