Introduction
Hibernate supports one-to-one mapping by using annotation on one-to-one elements. No foreign key is created in the primary table in such cases.
In the following mapping example, one employee may have only one address, and that address belongs to only one employee. We're using bidirectional association here.
Let’s start by learning about the persistent class.
Persistent Classes For One-to-One Mapping
Persistent Classes: Persistent classes in Hibernate are Java classes whose objects or instances will be persisted in database tables. Hibernate performs best when these classes adhere to a few simple conventions, generally known as the Plain Old Java Object (POJO) programming paradigm.
Employee.java and Address.java are two persistent classes in our one-to-one mapping hibernate project. Employee class includes a reference to Address class and vice versa.

Employee.java
package com.codingninjas;
import javax.persistence.*;
@Entity
@Table(name="employee")
public class Employee
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@PrimaryKeyJoinColumn
private int emp_Id;
private String emp_name,emp_email;
@OneToOne(targetEntity=Address.class,cascade=CascadeType.ALL)
private Address emp_address;
public int getemp_Id()
{
return emp_Id;
}
public void setemp_Id(int emp_Id)
{
this.emp_Id=emp_Id;
}
public String getemp_name()
{
return emp_name;
}
public void setemp_name(String emp_name)
{
this.emp_name=emp_name;
}
public String getemp_email()
{
return emp_email;
}
public void setemp_email(String emp_email)
{
this.emp_email=emp_email;
}
public Address getemp_address()
{
return emp_address;
}
public void setAddress(Address emp_address)
{
this.emp_address=emp_address;
}
}
Address.java
package com.codingninjas;
import javax.persistence.*;
@Entity
@Table(name="address")
public class Address
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int add_Id;
private String add_Line,add_city,add_state,add_country;
private int add_pincode;
@OneToOne(targetEntity=Employee.class)
private Employee employee;
public int getadd_Id()
{
return add_Id;
}
public void setadd_Id(int add_Id)
{
this.add_Id=add_Id;
}
public String getadd_Line()
{
return add_Line;
}
public void setadd_Line(String add_Line1)
{
this.add_Line=add_Line;
}
public String getadd_city()
{
return add_city;
}
public void setadd_city(String add_city)
{
this.add_city=add_city;
}
public String getadd_state()
{
return add_state;
}
public void setadd_state(String add_state)
{
this.add_state=add_state;
}
public String getadd_country()
{
return add_country;
}
public void setadd_country(String add_country)
{
this.add_country=add_country;
}
public int getadd_pincode()
{
return add_pincode;
}
public void setPincode(int add_pincode)
{
this.add_pincode=add_pincode;
}
public Employee getEmployee()
{
return employee;
}
public void setEmployee(Employee employee)
{
this.employee=employee;
}
}





