OneToOne Mapping in Hibernate using Annotations:

In this example lets see how to implement OneToOne mapping in hibernate using Annotations.

There are 3 types of Mappings in Hibernate,
1. OneToOne
2. ManyToOne

Here lets see an example of OntToOne Mapping.

In our example we are going to have 2 entities Address and Employee.  When we enter the employee details there will a column that points to another table ID where the related address of that employee is stored.


Once you go through this example, you can understand clearly.



    <property name="">update</property>
    <property name="hibernate.show_sql">true</property>  
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateannotation</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping class="com.javainfinite.pojo.Employee"/>
    <mapping class="com.javainfinite.pojo.Address"/>

package com.javainfinite.pojo;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

public class NewHibernateUtil {

    private static final SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
    public static SessionFactory getSessionFactory() {
        return sessionFactory;

package com.javainfinite.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


public class Address implements Serializable {
    private int address1;
    private String city;
    private String state;

    public int getAddress1() {
        return address1;

    public void setAddress1(int address1) {
        this.address1 = address1;

    public String getCity() {
        return city;

    public void setCity(String city) { = city;

    public String getState() {
        return state;

    public void setState(String state) {
        this.state = state;

package com.javainfinite.pojo;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

public class Employee {
    private int eid;
    private String ename;
    private String edept;
    @OneToOne (cascade={CascadeType.ALL})
    private Address address;

    public Address getAddress() {
        return address;

    public void setAddress(Address address) {
        this.address = address;

    public int getEid() {
        return eid;

    public void setEid(int eid) {
        this.eid = eid;
    public String getEname() {
        return ename;

    public void setEname(String ename) {
        this.ename = ename;

    public String getEdept() {
        return edept;

    public void setEdept(String edept) {
        this.edept = edept;

package com.javainfinite.execution;

import com.javainfinite.pojo.Address;
import com.javainfinite.pojo.Employee;
import com.javainfinite.pojo.NewHibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Execution {
  public static void main(String args[])
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        Transaction trans=session.beginTransaction();
        Employee emp=new Employee();
        Address addr=new Address();





Employee Table

Here we can see that a new column “Address_Joining” has been included in table and it points to ID: 1 of Address table


Here we can see the address table with pointing id:1



By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *