Hibernate One To One Mapping Using Annotations

Hibernate » on Sep 3, 2011 { 4 Comments } By Sivateja

Let us see the example on one to one mapping using annotations..

Files required…

  • hibernate.cfg.xml

package str;

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

public class Address {
	private  int addressId;

	private String place;

	private  Student  parent;

	public int getAddressId() {
		return addressId;
	public void setAddressId(int addressId) {
		this.addressId = addressId;
	public String getPlace() {
		return place;
	public void setPlace(String place) { = place;
	public Student getParent() {
		return parent;
	public void setParent(Student parent) {
		this.parent = parent;


package str;

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

public class Student {
	private int studentId;

	@Column(name="sname", length=10)
	private String studentName;

	private String grp;

	public String getGrp() {
		return grp;
	public void setGrp(String grp) {
		this.grp = grp;
	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;



<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
<property name="connection.url"></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="">update</property>


package str;

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

public class ClientForSave {
	public  static  void main(String args[])

		Configuration cfg=new Configuration();

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

		Student  s = new Student();

		Address  ad = new Address();

		Transaction  tx = session.beginTransaction();;
		System.out.println("One to One with annotations is done..!!!!");

Eclipse Output

In the database


You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


4 Responses to “Hibernate One To One Mapping Using Annotations”
  1. Ghazi says:

    Thank you for this example, I need to add a @OneToOne Adress attribute (type Adress) in Student class, which other annotations to use?
    Thank you

  2. sivaranjani says:

    Without having any file name on mapping resource ,how the flow to bean class will happen???

  3. kiran says:

    It's really great man.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.