A component mapping deals with entities involved in has - a relationship.

Component means “HAS_A_RELATIONSHIP”.
In this example we are describing component mapping i. e each employee has an unique address.

Employee Table

empno

ename

job

deptno

sal

1 Rex CLERK 10 12000
2 Ameet MGR 20 20000

Address Table

State

city

street

pincode

Karnataka

Bangalore

33A

545444

Karnataka

Mangalore

44B

656566

Address is a part of part of parent  table employee.  But without employee “Address” table has no existence. So it is better to map  “Address” table along  with “Employee “ Table.
After mapping “Address” to “ Employee”,  table will be like this.

empno

Number

ename

Varchar2

deptno

Number

job varchar2

sal

Number

state

Vharchar2

city

Vharchar2

street

Vharchar2

pincode

number

For achieving this relationship we have to follow these steps.

Step1:Create Employee and Address Class

Employee.java

package com.bullraider.componentmapping.employee;
package com.bullraider.componentmapping;
import java.io.Serializable;

public class Employee implements Serializable{
    private long empno;
    private String ename;
    private int sal;
    private String job;
    private int deptno ;
    private Address employeeAddress;

    Employee(){
    }
     public Employee( String ename, String job, int sal,int deptno, Address employeeAddress) {
        super();
         this.deptno = deptno;
         this.empno = empno;
         this.ename = ename;
         this.job = job;
         this.sal = sal;
         this.employeeAddress=employeeAddress;
    }
     public long getEmpno() {
        return empno;
    }
     public void setEmpno(long empno) {
         this.empno = empno;
    }
     public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
         this.ename = ename;
    }
    public int getSal() {
        return sal;
    }
    public void setSal(int sal) {
         this.sal = sal;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
         this.job = job;
    }
    public int getDeptno() {
        return deptno;
    }
    public void setDeptno(int deptno) {
         this.deptno = deptno;
    }
     public Address getEmployeeAddress() {
        return employeeAddress;
     }
     public void setEmployeeAddress(Address employeeAddress) {
         this.employeeAddress = employeeAddress;
    }
}

Address.java

package com.bullraider.componentmapping;

public class Address {
    private String street;
    private String city;
    private String state;
    private String pincode;
    Address(){
       
    }
    public Address(String city, String state, String street,String pincode) {
        super();
         this.city = city;
         this.pincode = pincode;
         this.state = state;
         this.street = street;
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
         this.street = street;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
         this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
         this.state = state;
    }
    public String getPincode() {
        return pincode;
    }
    public void setPincode(String pincode) {
         this.pincode = pincode;
    }

}

These two classes are separate but they will map to a single table i.e to Employee table

Step2: Design  " Employee.hbm.xml"  to create Employee table in Database.

Step3 Create hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.bullraider.componentmapping.Employee"
        table="emp100">
        <meta attribute="class-description"> This class contains employee details. </meta>
        <id name="empno" type="long" column="empno">
            <generator class="native" />
        </id>
        <property name="ename" type="string" column="ename" not-null="true" />
        <property name="job" type="string" column="job" not-null="true" />
        <property name="sal" type="integer" column="sal" not-null="true" />
        <property name="deptno" type="integer" column="deptno"
            not-null="true" />
        <component name="employeeAddress"
            class="com.bullraider.componentmapping.Address">
            <property name="street" column="street" type="string" />
            <property name="city" column="CITY" type="string" />
            <property name="state" column="STATE" type="string" />
            <property name="pincode" column="PINCODE" type="string" />
        </component>
    </class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="connection.password">tiger</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="show_sql">true</property>
       <property name="hbm2ddl.auto">create-drop</property>
        <mapping resource="com/bullraider/componentmapping/Employee.hbm.xmll"/>
    </session-factory>
</hibernate-configuration>

Step:4Create HibernateUtil class to get SessionFactory object.

package com.bullraider.componentmapping.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static {
        try {
             sessionFactory = new Configuration().configure()
                     .buildSessionFactory();
        } catch (Throwable ex) {
             System.err.println("SessionFactory creation failed" + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Step 5: Create a Class for  specifing operation

package com.bullraider.componentmapping;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

import com.bullraider.componentmapping.util.HibernateUtil;
public class Main{
     public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Long empId = null;
        try {
            transaction = session.beginTransaction();
           
            Address address1=new Address("Bangalore","Karnattaka","a001","560044");
            Employee emp1=new Employee("Prakash","SALESMAN",12000,10,address1);
            Address address2=new Address("Bangalore","Karnattaka","a001","560044");
            Employee emp2=new Employee("Pradeep","SALESMAN",12000,10,address2);
            Address address3=new Address("Bangalore","Karnattaka","a001","560044");
            Employee emp3=new Employee("Bijay","SALESMAN",12000,10,address3);
            Address address4=new Address("Bangalore","Karnattaka","a001","560044");
            Employee emp4=new Employee("Arpita","SALESMAN",12000,10,address4);
             session.save(emp1);
             session.save(emp2);
             session.save(emp3);
             session.save(emp4);
             transaction.commit();
             System.out.println("Records inserted sucessessfully");
            Main m=new Main();
             m.retriveEmployee();       
         } catch (HibernateException e) {
             transaction.rollback();
             e.printStackTrace();
        } finally {
             session.close();
        }}
    
    public void retriveEmployee()
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            List employee=session.createQuery("from Employee").list();
            for (Iterator iterator = employee.iterator(); iterator.hasNext();)
            {
                Employee employee1 = (Employee) iterator.next();
                 System.out.println(employee1.getEname()+"  "+
                         employee1.getEmployeeAddress().getState());//To get Employee's Name and State
            }          
             transaction.commit();
        } catch (HibernateException e) {
             transaction.rollback();
             e.printStackTrace();
        } finally {
             session.close();
        }
    }  
}

Download the code component_mapping.zip (