Hibernate One to One Mapping Example

Let us see few points regarding this one to one mapping..

  • One object is associated with one object only
  • In this relationship, one object of the one pojo class contains association with one object of the another pojo class
  • To apply one to one relationship between two pojo class objects it is possible by without taking a separate foreign key column in the child table of the database
  • To apply one to one relationship, we copy the primary key value of parent object into primary key value of the child object.  So that the relationship between two objects is one to one
  • If we want to copy parent object primary key value into child object primary key, we need to use a special generator class given by hibernate called foreign
  • Actually this foreign generator is only used in one to one relationship only
  • We are going to apply one to one between student and address pojo classes, here the relation is one address is assigned for one student only
  • In order to get one to one relationship between student and address, we are copying primary key value of student into primary key value of address

Example…..

files required…

  • Student.java
  • Address.java
  • Address.hbm.xml
  • Student.hbm.xml
  • OurLogic.java
  • hibernate.cfg.xml

Student.java

123456789101112131415161718192021package str;

public class Student {

	private int studentId;
	private String studentName;

	public int getStudentId() {
		return studentId;
	}
	public void setStudentId(int studentId) {
		this.studentId = studentId;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

}

Address.java

12345678910111213141516171819202122232425262728293031323334package str;
public class Address {

	private int addressId;
	private String city;
	private String state;
	private Student s;	

	public Student getS() {
		return s;
	}
	public void setS(Student s) {
		this.s = s;
	}
	public int getAddressId() {
		return addressId;
	}
	public void setAddressId(int addressId) {
		this.addressId = addressId;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}

}

Address.hbm.xml

1234567891011121314151617181920<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="str.Address" table="address">

<id name="addressId" column="addressid" >
<generator>
<param name="property">s</param>
</generator>
</id>
<property name="city" column="city" length="10"/>
<property name="state" column="state" length="10"/>

<one-to-one name="s" class="str.Student" cascade="all" />

</class>
</hibernate-mapping>

Student.hbm.xml

12345678910111213<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="str.Student" table="student">

<id name="studentId" column="studentid"  />
<property name="studentName" column="studentname" length="10"/>

</class>
</hibernate-mapping>

hibernate.cfg.xml

123456789101112131415161718192021<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<mapping resource="Student.hbm.xml"></mapping>
<mapping resource="Address.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

OurLogic.java

12345678910111213141516171819202122232425262728293031323334353637383940package str;

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

public class OurLogic {

	public static void main(String args[])
	{

		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml"); 

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();		

		Student s=new Student();
		s.setStudentId(100);
		s.setStudentName("java4s");

		Address ad = new Address();
		ad.setAddressId(509);
		ad.setCity("Carry");
		ad.setState("NC");
		ad.setS(s);		

		    Transaction tx = session.beginTransaction();

		              session.save(ad);

		    tx.commit();

		    session.close();
		    System.out.println("One to One is Done..!!");
		    factory.close();

	}
}

 

Let us see by loading the object form the database whether the one to one is working fine or not…

OurLogic_loading.java

123456789101112131415161718192021222324252627282930package str;

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

public class OurLogic_loading {

	public static void main(String args[])
	{

		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml"); 

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();		

        Object o = session.get(Address.class, new Integer(100));
        Address a = (Address)o;
        System.out.println(a.getCity());

        Student s=a.getS();
        System.out.println(s.getStudentName());

		    session.close();
		    System.out.println("One to One is Done..!!");
		    factory.close();

	}
}

 

Related Articles

post a comment