One-To-One Mapping

The One-To-One mapping represents a single-valued association where an instance of one entity is associated with an instance of another entity. In this type of association one instance of source entity can be mapped atmost one instance of target entity.

@OneToOne Example

In this example, we will create a One-To-One relationship between a Student and Library in such a way that one student can be issued only 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) and student name (s_name).

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;  
    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) and an object of student type marked with @OneToOne annotation.

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;  
  
@OneToOne  
private Student stud;

public Library(int b_id, String b_name, Student stud) {  
    super();  
    this.b_id = b_id;  
    this.b_name = b_name;  
    this.stud = stud;  
}  
  
public Library() {  
    super();  
      
}  
  
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;  
}  
  
public Student getStud() {  
    return stud;  
}  
  
public void setStud(Student stud) {  
    this.stud = stud;  
}    
}

Step 3.  Now, map the entity class and other databases confiuguration in Persistence.xml file.

Persistence.xml

<persistence>  
<persistence-unit name="Book_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 OneToOneExample under com.javahubpoint.OneToOne package to persist the entity object with data.

OneToOneExample.java

import javax.persistence.*;  
import com.javahubpoint.mapping.*;  
  
public class OneToOneExample {  
  
    public static void main(String[] args) {  
          
        EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Book_issued" );  
           EntityManager em = emf.createEntityManager( );  
           em.getTransaction( ).begin( );  
          
           Student st1=new Student();  
           st1.setS_id(1);  
           st1.setS_name("Vipul");  
             
           Student st2=new Student();  
           st2.setS_id(2);  
           st2.setS_name("Vimal");  
             
             
           em.persist(st1);  
           em.persist(st2);  
             
           Library lib1=new Library();  
           lib1.setB_id(101);  
           lib1.setB_name("Data Structure");  
           lib1.setStud(st1);  
             
           Library lib2=new Library();  
           lib2.setB_id(102);  
           lib2.setB_name("DBMS");  
           lib2.setStud(st2);  
             
           em.persist(lib1);  
           em.persist(lib2);  
             
           em.getTransaction().commit();  
             
           em.close();  
           emf.close();  
    }
}  

Output:

After the execution of the program, two 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 represents the mapping between student and library. To fetch data, run select * from library query in MySQL.