Javatpoint Logo
Javatpoint Logo

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.javatpoint;

public class Address {
private String city,country;
private int pincode;

//getters and setters
}
package com.javatpoint;
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.javatpoint.Employee" table="emp177">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="name"></property>

<component name="address" class="com.javatpoint.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



Please Share

facebook twitter google plus pinterest tumbler

Learn Latest Tutorials