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


files required…

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





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


You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web 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.


12 Responses to “Hibernate One to One Mapping Example”
  1. Mohammed Vaseem says:

    I got a big confusion in this program. what is the purpose of lines 10,11,2 in Address.hbm.xml files in this code.
    Please provide explaination to this program.
    Thanks in advance.

  2. MohanReddy says:

    Hi Admin,

    As Mohammed i got same doubt, what is the use of “10,11,12 in Address.hbm.xml” file
    2. in class what is the use of below line.

  3. naveen says:

    please mention class=”foreign” in generator class in Address.hbm.xml…otherwise you get some error…..i done…..

  4. king says:

    dear the programm you have zipped is many-many,can you please add one to one application

  5. naag says:

    please explain 10,11,12 lines in Address.hbm.xml

  6. rajgopal says:

    column=”studentname” but in screenshot it is studentnam ?
    I was creating table by looking at screenshots so was getting error
    but still I really like ;)

  7. Ganesh says:

    Dear Sir,

    I am Java Developer.
    Your website is very nice for student and Developer also.
    Sir can you please provide all Hibernate Example using anotaion .

    If possible please help me doing this.

    Thanks …..

    Ganesh ….!

  8. raghava says:

    hi siva ,
    can u please clarify line no 24
    whats need?

  9. kamorudeen says:

    thanks so much for this example it help……………you guys asking question should register for a java class,it will help you….in case you need an answer those number 10,10 are the size of the word you will enter in byte code…why the address 509,is the id in the table in data bass…register for a java class fast!!!

  10. Ramesh says:

    Really below code so confusing.
    I can see, so many has the same doubts, can any one clarify on this. even attached source code also wrong code.

    Address.hbm.xml — 10,11,12 — 24


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

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