Hibernate One To One Relation Mapping Example

Project Structure

Now project structure looks like following

Hibernate One To One Mapping Example

Just execute below class and see the output

HibernateOneToOneExample.java

package com.hibernate.onetoone;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class HibernateOneToOneExample {

   
public static void main(String[] args) {
       
Session session = HibernateUtil.createSessionFactory().openSession();
        Transaction transaction =
null;
       
try {
           
transaction = session.beginTransaction();
          
            Address address1 =
new Address("Padma Junction", "Cochin",
                   
"Kerala", "India");
            Address address2 =
new Address("Eden Road", "Kolkota", "Bengal",
                   
"India");
            Employee employee1 =
new Employee("Rockey", address1);
            Employee employee2 =
new Employee("Jose", address2);
          
            session.save
(employee1);
            session.save
(employee2);
            transaction.commit
();
       
catch (Exception e) {
           
transaction.rollback();
       
}finally {
           
session.close();
       
}
    }

}
Output
ADDRESSID CITY COUNTRY STATE STREET
1 Cochin India Kerala Padma Junction
2 Kolkota India Bengal Eden Road
EMPLOYEEID EMPLOYEENAME EMPLOYEE_ADDRESS
1 Rockey 1
2 Jose 2

 





Previous 1 2 3








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