What is component mapping in hibernate?

Questions by sunils   answers by sunils

Showing Answers 1 - 7 of 7 Answers

Rajavel.B

  • Nov 6th, 2006
 

A component is a contained object that is persisted as a value type ,not an entity reference.eg)

public class person{

private Name name;

public Name getName(){ return name;}

public void setName(Name name){this.name=name;}

.....

}

public class Name

{chat initial;

String first;

String last;

public char getInitial(){return initial;}

public void setInitial(char initial){this.initial=initial;}

.......//first,last

}

Now 'Name' may be persited to the component of 'person'

in hbm:

<class name="eg.Person" table="person">    <id name="Key" column="pid" type="string">        <generator class="uuid"/>    </id>    <property name="birthday" type="date"/>    <component name="Name" class="eg.Name"> <!-- class attribute optional -->        <property name="initial"/>        <property name="first"/>        <property name="last"/>    </component></class>

The person table would have the columns pid, birthday, initial, first and last.

  Was this answer useful?  Yes

I will explain one realtime scenario here.Consider one Address class as given below
public class Address{
private Integer doorNo;
private String street;
//provide setters and getters for above properties
}

Consider another class like StudentDetails
public class StudentDetails{
private Integer sno;
private String sname;
private Address address;
//provide setters and getters for above properties
}

So in the above situation StudentDetails class is using Address class also how we are going to configure in hbm file is important

<//code for normal StudentDetails  >
<component name="address" class="packagename.modulename.Address">
         --Normal mapping for addess--
</component>

  Was this answer useful?  Yes

Thomas John

  • Sep 21st, 2015
 

Hibernate component represents as a group of values or properties, not entity (table). I have a customer table that has address fields. I will create 2 models - customer, address. In this case, Address.java is a "component" represent the "Address1", "Address2" and "Address3" columns for Customer.java

Code
  1.  

  2. <component name="Address" class="com.mkyong.customer.Address">

  3.                         <property name="address1" type="string">

  4.                                 <column name="ADDRESS1" not-null="true" />

  5.                         </property>

  6.                         <property name="address2" type="string">

  7.                                 <column name="ADDRESS2" not-null="true" />

  8.                         </property>

  9.                         <property name="address3" type="string">

  10.                                 <column name="ADDRESS3" not-null="true" />

  11.                         </property>

  12. </component>

  13.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions