Home » Hibernate Component Mapping Example

Hibernate Component Mapping Example

by Online Tutorials Library

Component Mapping

In component mapping, we will map the dependent object as a component. An component is an object that is stored as an value rather than entity reference. This is mainly used if the dependent object doen’t have primary key. It is used in case of composition (HAS-A relation), that is why it is termed as component. Let’s see the class that have HAS-A relationship.

package com.tutoraspire;  public class Address { private String city,country; private int pincode;  //getters and setters } 
package com.tutoraspire; public class Employee { private int id; private String name; private Address address;//HAS-A  //getters and setters }  

Here, address is a dependent object. Hibernate framework provides the facility to map the dependent object as a component. Let’s see how can we map this dependent object in mapping file.

... <class name="com.tutoraspire.Employee" table="emp177"> <id name="id"> <generator class="increment"></generator> </id> <property name="name"></property>  <component name="address" class="com.tutoraspire.Address"> <property name="city"></property> <property name="country"></property> <property name="pincode"></property> </component>  </class> ...  

Let’s see the data of the emp177 table.

hibernate component mapping

You may also like