×
☰ See All Chapters

JPA One-to-One Bidirectional Association Mapping

One-To-One mapping is an association between one persistence object and another one related persistence object. If one persistence object uses other and in back if other using the first persistence object then it becomes bidirectional.  If one persistence object uses other and in back if other is not using the first persistence object then it becomes unidirectional.  As an example, the association between, Citizen and Passport, here one citizen has one Passport. Citizen needs Passport and also Passport does need Citizen and hence it becomes bidirectional association.

JPA One-to-One Bidirectional Association Mapping Example

Now let us see how this mapping is done in persistence class by going through an example.  The following code shows how to do one-to-one unidirectional association mapping. We created two entities, Citizen and Passport. In the Citizen class we annotate the Passport field by @OneToOne annotation and also in Passport class we annotated the Citizen field by @OneToOne annotation.

Database script (MySQL)

DROP TABLE CITIZEN;

COMMIT;

 

DROP TABLE PASSPORT;

COMMIT;

 

CREATE TABLE PASSPORT (

  PID INT(5) PRIMARY KEY AUTO_INCREMENT,

  PNUMBER VARCHAR(30)

);

 

CREATE TABLE CITIZEN (

  CID INT(5) PRIMARY KEY AUTO_INCREMENT,

  CNAME VARCHAR(30),

  PID INT(5),

  CONSTRAINT FOREIGN KEY (PID) REFERENCES PASSPORT (PID)

);

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>121_BidirectionalAssociationMapping</artifactId>

        <packaging>jar</packaging>

        <version>1.0-SNAPSHOT</version>

        <name>121_BidirectionalAssociationMapping</name>

        <url>https://maven.apache.org</url>

        <dependencies>

                <dependency>

                        <groupId>junit</groupId>

                        <artifactId>junit</artifactId>

                        <version>3.8.1</version>

                        <scope>test</scope>

                </dependency>

                <dependency>

                        <groupId>org.eclipse.persistence</groupId>

                        <artifactId>javax.persistence</artifactId>

                        <version>2.0.0</version>

                </dependency>

 

                <dependency>

                        <groupId>org.hibernate</groupId>

                        <artifactId>hibernate-entitymanager</artifactId>

                        <version>4.2.8.Final</version>

                </dependency>

 

                <dependency>

                        <groupId>mysql</groupId>

                        <artifactId>mysql-connector-java</artifactId>

                        <version>8.0.11</version>

                </dependency>

        </dependencies>

</project>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="https://java.sun.com/xml/ns/persistence"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://java.sun.com/xml/ns/persistence

             https://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"

        version="2.0">

 

        <persistence-unit name="CitizenPU">

                <provider>org.hibernate.ejb.HibernatePersistence</provider>

                <properties>

                        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/study" />

                        <property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver" />

                        <property name="hibernate.connection.username" value="root" />

                        <property name="hibernate.connection.password" value="root" />

                        <property name="hibernate.archive.autodetection" value="class" />

                        <property name="hibernate.show_sql" value="true" />

                        <property name="hibernate.format_sql" value="true" />

                        <property name="hbm2ddl.auto" value="update" />

                </properties>

        </persistence-unit>

</persistence>

Citizen.java

package com.java4coding;

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.OneToOne;

import javax.persistence.PrimaryKeyJoinColumn;

import javax.persistence.Table;

 

@Entity

@Table(name = "CITIZEN")

public class Citizen {

       

        @Id

        @GeneratedValue(strategy = GenerationType.AUTO)

        @Column(name = "CID")

        private int cid;

       

        @Column(name = "CNAME")

        private String cname;

       

        @OneToOne

      @PrimaryKeyJoinColumn

        private Passport passport;

 

        public int getCid() {

                return this.cid;

        }

        public void setCid(int cid) {

                this.cid = cid;

        }

        public String getCname() {

                return this.cname;

        }

 

        public void setCname(String cname) {

                this.cname = cname;

        }

        public Passport getPassport() {

                return passport;

        }

        public void setPassport(Passport passport) {

                this.passport = passport;

        }

}

Passport.java

package com.java4coding;

 

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.OneToOne;

import javax.persistence.Table;

 

@Entity

@Table(name = "PASSPORT")

public class Passport {

       

        @Id

        @GeneratedValue(strategy = GenerationType.AUTO)

        @Column(name = "PID")

        private int pid;

       

        @Column(name = "PNUMBER")

        private String pnumber;

       

        @OneToOne( mappedBy = "passport", cascade = CascadeType.ALL)

        @JoinColumn(name="PID")

        private Citizen citizen;

 

        //setters and getters

        public int getPid() {

                return this.pid;

        }

        public void setPid(int pid) {

                this.pid = pid;

        }

        public String getPnumber() {

                return this.pnumber;

        }

        public void setPnumber(String pnumber) {

                this.pnumber = pnumber;

        }

        public Citizen getCitizen() {

                return citizen;

        }

        public void setCitizen(Citizen citizen) {

                this.citizen = citizen;

        }

}

Test.java

package com.java4coding;

 

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

 

public class Test {

        public static void main(String[] args) {

                EntityManagerFactory emf = Persistence.createEntityManagerFactory("CitizenPU");

                EntityManager em = emf.createEntityManager();

 

                em.getTransaction().begin();

 

                Citizen c = new Citizen();

                c.setCname("Manu Manjunatha");

 

                Passport passport = new Passport();

                passport.setPnumber("123456");

                // ASSOCIATE CITIZEN WITH PASSPORT

                passport.setCitizen(c);

               

                // ASSOCIATE PASSPORT WITH CITIZEN

                c.setPassport(passport);

               

                em.persist(passport);

                em.getTransaction().commit();

               

                em.getTransaction().begin();

                // CITIZEN CAN ACCESS PASSPORT

                Citizen cz = em.find(Citizen.class,1);

                System.out.println("Citizen id is " + cz.getCid());

                System.out.println("Citizen name is " + cz.getCname());

                System.out.println("Citizen passport number is " + cz.getPassport().getPnumber());

               

                // PASSPORT CAN ACCESS CITIZEN BECAUSE IT IS BIDIRECTIONAL

                Passport pt = em.find(Passport.class,1);

                System.out.println("Passport id is " + pt.getPid());

                System.out.println("Passport number is " + pt.getPnumber());

                System.out.println("Citizen name is " + pt.getCitizen().getCname());

               

                em.getTransaction().commit();

        }

}

 

Project directory structure

jpa-one-to-one-bidirectional-association-mapping-0
 

Output:

Hibernate:

    insert

    into

        PASSPORT

        (PNUMBER)

    values

        (?)

Hibernate:

    insert

    into

        CITIZEN

        (CNAME)

    values

        (?)

Citizen id is 1

Citizen name is Manu Manjunatha

Citizen passport number is 123456

Passport id is 1

Passport number is 123456

Citizen name is Manu Manjunatha

 


All Chapters
Author