OneToOne mapping example

In this example we will see how to establish one to one relationship in hibernate. To explain this, lets take two classes person and passport. Each person will have one passport only and each passport will have one person details only.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">hr</property>
        <property name="connection.password">hr</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.ram.dao.Person"/>
        <mapping class="com.ram.dao.Passport"/>

    </session-factory>

</hibernate-configuration>
File: com.ram.dao.Passport.java
package com.ram.dao;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Passport {
 @Id
 private String passportNo;
 @Temporal(TemporalType.DATE)
 private Date dateOfIssue;
 @Temporal(TemporalType.DATE)
 private Date dateOfExpiry;
 
 public String getPassportNo() {
  return passportNo;
 }
 public void setPassportNo(String passportNo) {
  this.passportNo = passportNo;
 }
 public Date getDateOfIssue() {
  return dateOfIssue;
 }
 public void setDateOfIssue(Date dateOfIssue) {
  this.dateOfIssue = dateOfIssue;
 }
 public Date getDateOfExpiry() {
  return dateOfExpiry;
 }
 public void setDateOfExpiry(Date dateOfExpiry) {
  this.dateOfExpiry = dateOfExpiry;
 }
}

File: com.ram.dao.Person.java
package com.ram.dao;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Person {
 @Id @GeneratedValue
 private int person_id;
 private String firstName;
 private String lastName;
 @OneToOne
 private Passport passport;
 
 public Passport getPassport() {
  return passport;
 }
 public void setPassport(Passport passport) {
  this.passport = passport;
 }
 public int getPerson_id() {
  return person_id;
 }
 public void setPerson_id(int person_id) {
  this.person_id = person_id;
 }
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
}

File: com.ram.hibernate.OneToOneExample.java
package com.ram.hibernate;

import java.text.SimpleDateFormat;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.ram.dao.Passport;
import com.ram.dao.Person;

public class OneToOneExample {
 public static void main(String[] args) throws Exception{
  
  String dateOfIssue = "01/01/2010";
  String dateOfExpiry = "01/01/2020";
  
  Passport passport = new Passport();
  passport.setPassportNo("ABC123");
  passport.setDateOfIssue(new SimpleDateFormat("dd/mm/yyyy").parse(dateOfIssue));
  passport.setDateOfExpiry(new SimpleDateFormat("dd/mm/yyyy").parse(dateOfExpiry));
  
  Person person = new Person();
  person.setFirstName("Ram");
  person.setLastName("Akunuru");
  person.setPassport(passport);
  
  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  
  session.beginTransaction();
  session.save(person);
  session.save(passport);
  session.getTransaction().commit();
  session.close();
  
 }
}


Execute the OneToOneExample.java and you get the below output.

No comments:

Post a Comment