91-9990449935 0120-4256464 |
Component MappingIn 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.trainingtrains; public class Address { private String city,country; private int pincode; //getters and setters } package com.trainingtrains; 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.trainingtrains.Employee" table="emp177"> <id name="id"> <generator class="increment"></generator> </id> <property name="name"></property> <component name="address" class="com.trainingtrains.Address"> <property name="city"></property> <property name="country"></property> <property name="pincode"></property> </component> </class> ... Let's see the data of the emp177 table.
download this hibernate example (developed using Myeclipse IDE)
download this hibernate example (developed using Eclipse IDE) |