one to one mapping in hibernate | one to one relationship | foreign   

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
One to one is a relationship in relational database, it will occur when a parent table record has zero or one child record in child table. In this tutorial, we are going to implement the one to one mapping in hibernate (relationship) using xml configuration.
One to one mapping in hibernate can be achieved in two ways.
One to one mapping with foreign key
One to one mapping with primary key
In this example, we are implementing the one to one mapping with foreign key using xml configuration.
One to one mapping in hibernate is like many to one relationship, but in the many to one relationship a foreign key column can allow duplicate values. If we prevent the duplicate values in foreign key column then it will act as one to one mapping.
To do so.. To prevent the duplicates in foreign key column, hibernate provides two attributes like unique=”true” and not-null=”true”. We can apply these attributes in <many-to-one> in hibernate mapping file (hbm.xml).
In order to get one to one relationship between two objects, in child class pojo class we need a reference variable of type parent class.
If we take the Person and Passport, there is a relationship between Person to Passport is one to one relationship. Because one Person can have only one Passport. We can implement this relationship using hibernate one to one mapping.
Project Structure :
Required Dependencies :
<dependencies>
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.0.Final</version>
        </dependency>
        <!-- MySQL Driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.5</version>
        </dependency>
</dependencies>
Hibernate POJO Classes :
Person.java
package com.onlinetutorialspoint.hibernate.pojo;

public class Person {
	private int personId;
	private String personName;
	public int getPersonId() {
		return personId;
	}
	public void setPersonId(int personId) {
		this.personId = personId;
	}
	public String getPersonName() {
		return personName;
	}
	public void setPersonName(String personName) {
		this.personName = personName;
	}
	
}

Passport.java
package com.onlinetutorialspoint.hibernate.pojo;

import java.util.Date;

public class Passport {
    private int passportNumber;
    private Date issudDate;
    private Date expireDate;
    private Person person;
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
    public int getPassportNumber() {
        return passportNumber;
    }
    public void setPassportNumber(int passportNumber) {
        this.passportNumber = passportNumber;
    }
    public Date getIssudDate() {
        return issudDate;
    }
    public void setIssudDate(Date issudDate) {
        this.issudDate = issudDate;
    }
    public Date getExpireDate() {
        return expireDate;
    }
    public void setExpireDate(Date expireDate) {
        this.expireDate = expireDate;
    }

}
Hibernate Mapping Files :
person.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.onlinetutorialspoint.hibernate.pojo.Person" table="person">
        <id name="personId" column="personid" />
        <property name="personName" column="personname" />
    </class>
</hibernate-mapping>
passport.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.onlinetutorialspoint.hibernate.pojo.Passport"
        table="passport">
        <id name="passportNumber" column="ppid" />
        <property name="issudDate" column="idate" />
        <property name="expireDate" column="edate" />
        <many-to-one name="person" class="com.onlinetutorialspoint.hibernate.pojo.Person"
        column="per_id" unique="true" not-null="true" cascade="all"/>
 </class>
</hibernate-mapping>
Hibenate Utility:
HibernateUtil.java
package com.onlinetutorialspoint.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private HibernateUtil() {

    }

    private static SessionFactory sessionFactory;

    public static synchronized SessionFactory getInstnce() {

        if (sessionFactory == null) {
            Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
        }
        return sessionFactory;

    }
}
Well! we are almost done.
Run the application
Main.java
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.onlinetutorialspoint.hibernate.pojo.Passport;
import com.onlinetutorialspoint.hibernate.pojo.Person;
import com.onlinetutorialspoint.util.HibernateUtil;

public class Main {

    public static void main(String[] args) {

        SessionFactory sessionFactory = HibernateUtil.getInstnce();
        Session session = sessionFactory.openSession();

        Person person = new Person();
        person.setPersonId(1001);
        person.setPersonName("Chandrashekhar");

        Passport passport = new Passport();
        passport.setPassportNumber(12346856);
        passport.setExpireDate(new Date());
        passport.setIssudDate(new Date());
        passport.setPerson(person);

        Transaction transaction = session.beginTransaction();
        session.save(passport);
        transaction.commit();

        session.close();
        sessionFactory.close();
    }
}
Output :
Hibernate: select person_.personid, person_.personname as personna2_1_ from person person_ where person_.personid=?
Hibernate: insert into person (personname, personid) values (?, ?)
Hibernate: insert into passport (idate, edate, per_id, ppid) values (?, ?, ?, ?)
Database Output :
Happy Learning 🙂


One to One Mapping in Hibernate Example

File size: 20 KB
Downloads: 818


Hibernate One to One Mapping using primary key (XML)
Hibernate Many to Many Mapping Example (XML)
Hibernate Composite Key Mapping Example
Hibernate One To Many Example (XML Mapping)
Many to One Mapping in Hibernate Example
Hibernate Filter Example Xml Configuration
hbm2ddl.auto Example in Hibernate XML Config
Basic Hibernate Example with XML Configuration
Calling Stored Procedures in Hibernate
Hibernate Right Join Example
Hibernate One To Many Using Annotations
Custom Generator Class in Hibernate
Hibernate Inheritance Mapping Strategies
hibernate update query example
Table per Concrete Class in Hibernate Inheritance

Hibernate One to One Mapping using primary key (XML)
Hibernate Many to Many Mapping Example (XML)
Hibernate Composite Key Mapping Example
Hibernate One To Many Example (XML Mapping)
Many to One Mapping in Hibernate Example
Hibernate Filter Example Xml Configuration
hbm2ddl.auto Example in Hibernate XML Config
Basic Hibernate Example with XML Configuration
Calling Stored Procedures in Hibernate
Hibernate Right Join Example
Hibernate One To Many Using Annotations
Custom Generator Class in Hibernate
Hibernate Inheritance Mapping Strategies
hibernate update query example
Table per Concrete Class in Hibernate Inheritance






Milana Travis
October 25, 2017 at 11:21 am - Reply 

Thank you very much for your blog.
I enjoyed reading this article.










Milana Travis
October 25, 2017 at 11:21 am - Reply 

Thank you very much for your blog.
I enjoyed reading this article.




Thank you very much for your blog.
I enjoyed reading this article.
 


Δ
 Hibernate – Introduction
 Hibernate – Advantages
 Hibernate – Download and Setup
 Hibernate – Sql Dialect list
 Hibernate – Helloworld – XML
 Hibernate – Install Tools in Eclipse
 Hibernate – Object States
 Hibernate – Helloworld – Annotations
 Hibernate – One to One Mapping – XML
 Hibernate – One to One Mapping foreign key – XML
 Hibernate – One To Many -XML
 Hibernate – One To Many – Annotations
 Hibernate – Many to Many Mapping – XML
 Hibernate – Many to One – XML
 Hibernate – Composite Key Mapping
 Hibernate – Named Query
 Hibernate – Native SQL Query
 Hibernate – load() vs get()
 Hibernate Criteria API with Example
 Hibernate – Restrictions
 Hibernate – Projection
 Hibernate – Query Language (HQL)
 Hibernate – Groupby Criteria HQL
 Hibernate – Orderby Criteria
 Hibernate – HQLSelect Operation
 Hibernate – HQL Update, Delete
 Hibernate – Update Query
 Hibernate – Update vs Merge
 Hibernate – Right Join
 Hibernate – Left Join
 Hibernate – Pagination
 Hibernate – Generator Classes
 Hibernate – Custom Generator
 Hibernate – Inheritance Mappings
 Hibernate – Table per Class
 Hibernate – Table per Sub Class
 Hibernate – Table per Concrete Class
 Hibernate – Table per Class  Annotations
 Hibernate – Stored Procedures
 Hibernate – @Formula Annotation
 Hibernate – Singleton SessionFactory
 Hibernate – Interceptor
 hbm2ddl.auto Example in Hibernate XML Config
 Hibernate – First Level Cache