One-to-One Example Hibernate

One-to-One Example Hibernate
  1. One-To-one Relationship: In one-to-one relationship, one record in one table is related to exactly to one record in another table. The two tables are associated with a foreign key. That is, the primary key of one table is the foreign key in another table.
  2. Concept: One Student contains only one residential address. Here, Student class maps to studentinfo table and Address class maps to addressinfo table.
  3. Association in tables: sid exists in both the tables. sid is declared as primary key in studentinfo table and the same sid is declared as foreign key in addressinfo table. That is, sid exists in both the tables.
  4. Bean coding: Two beans exist, Student and Address. As Student have one to one relationship with Address and as well Address have one to one relationship with Student, one object of Address is created in Student class and also one object of Student is created in Address class.

Following table gives the variables of Student and Address classes.

Student Address
private int sid; private int sid;
private String sname; private String flatno, plotno, street, city;
private Address address; private Student student;

Notice the following observations:

  1. sid exists in both classes
  2. Address object is created in Student and Student object is created in Address. This is how one-to-one relationship is represented in bean classes. In database it is easier. In foreign key one to one mapping, two entities will have one-to-one association using foreign keys.

Following is the ER diagram of tables studentinfo and addressinfo.

One-to-One Example Hibernate

1. Bean Program: Student.java

public class Student 
{
  private int sid;
  private String sname;
  private Address address;

  public Student() 
  {
    super();
  }
  public Student(int sid, String sname) 
  {
    super();
    this.sid = sid;
    this.sname = sname;
  }
  public int getSid() 
  {
    return sid;
  }
  public void setSid(int sid) 
  {
    this.sid = sid;
  }
  public String getSname() 
  {
    return sname;
  }
  public void setSname(String sname) 
  {
    this.sname = sname;
  }
  public Address getAddress() 
  {
    return address;
  }
  public void setAddress(Address address) 
  {
    this.address = address;
  }
  public String toString() 
  {
    return "sid=" + sid + ", sname=" + sname ;
  }
}

2. Bean Program: Address.java

public class Address 
{
  private int sid;
  private String flatno, plotno, street, city;
  private Student student;

  public Address() 
  {
    super();
  }
  public Address(String flatno, String plotno, String street, String city, Student student) 
  {
    super();
    this.street=street;
    this.flatno = flatno;
    this.plotno = plotno;
    this.city = city;
    this.student = student;
  }
  public int getSid() 
  {
    return sid;
  }
  public void setSid(int sid) 
  {
    this.sid = sid;
  }
  public String getFlatno() 
  {
    return flatno;
  }
  public void setFlatno(String flatno) 
  {
    this.flatno = flatno;
  }
  public String getPlotno() 
  {
    return plotno;
  }
  public void setPlotno(String plotno) 
  {
    this.plotno = plotno;
  }
  public String getStreet() 
  {
    return street;
  }
  public void setStreet(String street) 
  {
    this.street = street;
  }
  public String getCity() 
  {
    return city;
  }
  public void setCity(String city) 
  {
    this.city = city;
  }
  public Student getStudent() 
  {
    return student;
  }
  public void setStudent(Student student) 
  {
    this.student = student;
  }
  public String toString() 
  {
    return "city=" + city + ", flatno=" + flatno + ", plotno=" + plotno + ", sid=" + sid ;
  }    
}

3. Configuration file: hibernate.cfg.xml






  
    org.hibernate.dialect.Oracle9Dialect
  
  jdbc:oracle:thin:@localhost:1521:orcl1
  scott
  tiger
   oracle.jdbc.driver.OracleDriver
  SNRaoConnection
  true
  create

  



4. Mapping file: Student.hbm.xml





  
    
      
        student_seq
      
    
    
    
  

  
    
      
        student
      
    
    
    
    
    
    
    
  

<generator class="foreign">
constrained="true"

To ensure that sid of table gets a value of sid from studentinfo, a special id generation strategy foreign is used. It is very specific to one-to-one relationship. To declare the foreign key constraint on sid of addressinfo, constrained="true" is set.

<one-to-one name="address" class="Address"></one-to-one>

address is a variable in Student class.

<one-to-one name="student" class="Student" constrained="true"></one-to-one>

student is a variable in Address class.

5. Client file: InsertClient.java

Let us write one Java client class to check one-to-one relationship is working normally. The program should insert records in both tables when one Address object is saved.

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

public class InsertClient 
{
  public static void main(String[] args) 
  {
    Configuration c = new Configuration();
    c.configure("/hibernate.cfg.xml");
    SessionFactory sf = c.buildSessionFactory();
    Session s = sf.openSession();
    Transaction t = s.beginTransaction();

    Student s1 = new Student(0,"S N Rao");
    Address a1 = new Address("301","15","Jawahar Nagar","Hyderabad",s1);

    s.save(a1);
    s.flush();
    t.commit();
    System.out.println("Record of " + s1.getSid() + " is inserted");
  }
}

Student s1 = new Student(0,"S N Rao");
Address a1 = new Address("301", "15", "Jawahar Nagar", "Hyderabad", s1);

Student sid is given as 0, but 1 is inserted as per the sequence generated by the Hibernate. To Address object a1, the Student object s1 is added as parameter in the constructor. Now when, a1 is saved, not only a1 data is stored in addressinfo table but also s1 is saved into studentinfo table.

Following is the Console screen of MyEclipse when the above client program is executed. Observe, the SQL statements executed by Hibernate – one for Student object and one for Address object.

One-to-One Example Hibernate

Following is the Oracle table when the record is inserted.

One-to-One Example Hibernate

    All Associations:

  1. One-to-Many
  2. Many-to-One
  3. One-to-One
  4. Many-to-Many (with Set)

View All Hibernate Examples

3 thoughts on “One-to-One Example Hibernate”

Leave a Comment

Your email address will not be published.