One-to-One Example Hibernate

One-to-One Example Hibernate

One-to-One Example Hibernate
  1. One-To-one Relationship: In one-to-one relationship, one record in one table is related to exactly to one record in another table. The two tables are associated with a foreign key. That is, the primary key of one table is the foreign key in another table.
  2. Concept: One Student contains only one residential address. Here, Student class maps to studentinfo table and Address class maps to addressinfo table.
  3. Association in tables: sid exists in both the tables. sid is declared as primary key in studentinfo table and the same sid is declared as foreign key in addressinfo table. That is, sid exists in both the tables.
  4. Bean coding: Two beans exist, Student and Address. As Student have one to one relationship with Address and as well Address have one to one relationship with Student, one object of Address is created in Student class and also one object of Student is created in Address class.

Following table gives the variables of Student and Address classes.

Student Address
private int sid; private int sid;
private String sname; private String flatno, plotno, street, city;
private Address address; private Student student;

Notice the following observations:

  1. sid exists in both classes
  2. Address object is created in Student and Student object is created in Address. This is how one-to-one relationship is represented in bean classes. In database it is easier. In foreign key one to one mapping, two entities will have one-to-one association using foreign keys.

Following is the ER diagram of tables studentinfo and addressinfo.

One-to-One Example Hibernate

1. Bean Program:

2. Bean Program:

3. Configuration file: hibernate.cfg.xml

4. Mapping file: Student.hbm.xml

<generator class="foreign">

To ensure that sid of table gets a value of sid from studentinfo, a special id generation strategy foreign is used. It is very specific to one-to-one relationship. To declare the foreign key constraint on sid of addressinfo, constrained="true" is set.

<one-to-one name="address" class="Address"></one-to-one>

address is a variable in Student class.

<one-to-one name="student" class="Student" constrained="true"></one-to-one>

student is a variable in Address class.

5. Client file:

Let us write one Java client class to check one-to-one relationship is working normally. The program should insert records in both tables when one Address object is saved.

Student s1 = new Student(0,"S N Rao");
Address a1 = new Address("301", "15", "Jawahar Nagar", "Hyderabad", s1);

Student sid is given as 0, but 1 is inserted as per the sequence generated by the Hibernate. To Address object a1, the Student object s1 is added as parameter in the constructor. Now when, a1 is saved, not only a1 data is stored in addressinfo table but also s1 is saved into studentinfo table.

Following is the Console screen of MyEclipse when the above client program is executed. Observe, the SQL statements executed by Hibernate – one for Student object and one for Address object.

One-to-One Example Hibernate

Following is the Oracle table when the record is inserted.

One-to-One Example Hibernate

    All Associations:

  1. One-to-Many
  2. Many-to-One
  3. One-to-One
  4. Many-to-Many (with Set)

View All Hibernate Examples

2 thoughts on “One-to-One Example Hibernate

Leave a Reply

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