component mapping in hibernate

Hi,

I would like to know more about component mapping in hibernate. How is it different from regular mapping.
http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-component-1.html

>>>Hibernate terms the Address entity is called the component and it cannot have its own primary key, it uses the primary key of the enclosing Student entity.
I have not understood above statement also.

When where and why to use it. Any ideas, resources, links, sample code highly appreciated. thanks in advance.
LVL 7
gudii9Asked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:


This seems understandable to me - if you know that according to your
design student can have only one address, then it is more efficient not to have spearate
tables to store properties of student and properties of address, but to put them into one table.
that is because once you stated that each student has one address it means each student has only one street, one zip code,one city
in the same way as he/she have one date of birth, and say one last name.
Then you communicate this fact to hibernate and it will know that
both personal information about student and information about her address you
can store in one table - so you don't need separate address table, so it will not have
separat primary key.
This relationship between entity of student and entity of address in the laguage of hibernate
they say that address entity is the componenet,
even though in Java class Student and class Addrres
may be different.

In normal most straightforward situation hibernate will map ech class to its own table.
 

0
 
Sathish David Kumar NConnect With a Mentor ArchitectCommented:
I will explain  that ...  Address is one class having there deatils like

public class Address{
private Integer doorNo;
private String street;
private String add1;
private String add2;

//getter and setter method
}

Now think u have student  class that have the refreneces of Address calss
this called compent enity
public class StudentDetails{
private Integer no;
private String sFname;
private String sLname;

private Address address;
//setters and getters method
}

In mapping of student xml file fro address  you define like this ...

<component name="address" class="packagename.modulename.Address">
</component>
 
0
 
Sathish David Kumar NArchitectCommented:


why we go for Compente entity mapping means ....

Its use of normalization purpose ...that means  the address class is used in another class other than student calss (code reuseablity)

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
gudii9Author Commented:






>>>

Student.hbm.xml is used to create the STUDENT table.
01.
<?xml version="1.0"?>
02.
<!DOCTYPE hibernate-mapping PUBLIC
03.
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
04.
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05.
<hibernate-mapping>
06.
<class name="com.vaannila.student.Student" table="STUDENT">
07.
<meta attribute="class-description">This class contains student details.</meta>
08.
<id name="studentId" type="long" column="STUDENT_ID">
09.
<generator class="native" />
10.
</id>
11.
<property name="studentName" type="string" not-null="true"
12.
column="STUDENT_NAME" />
13.
<component name="studentAddress" class="com.vaannila.student.Address">
14.
<property name="street" column="ADDRESS_STREET" type="string"
15.
length="250" />
16.
<property name="city" column="ADDRESS_CITY" type="string"
17.
length="50" />
18.
<property name="state" column="ADDRESS_STATE" type="string"
19.
length="50" />
20.
<property name="zipcode" column="ADDRESS_ZIPCODE" type="string"
21.
length="10" />
22.
</component>
23.
</class>
24.
</hibernate-mapping




in above student.hbm.xml how we know what are columns come under property

<property name="studentName" type="string" not-null="true"

column="STUDENT_NAME" />


what comes under component like

<component name="studentAddress" class="com.vaannila.student.Address">

<property name="street" column="ADDRESS_STREET" type="string"

length="250" />

<property name="city" column="ADDRESS_CITY" type="string"

length="50" />

<property name="state" column="ADDRESS_STATE" type="string"

length="50" />

<property name="zipcode" column="ADDRESS_ZIPCODE" type="string"

length="10" />

</component>


also can you please elaborate on

>>>Its use of normalization purpose ...that means  the address class is used in another class other than student calss (code reuseablity)

i was not clear on what u mean by normalization and reusability. please advise
0
 
gudii9Author Commented:

>>>This relationship between entity of student and

entity of address in the laguage of hibernate
they say that address entity is the componenet,

even though in Java class Student and class Addrres
may be different.






Do we need separate java classes for Student and Address in this case since we are taking care of Student and Address in single table itself as student can have only single address.
>>In normal most straightforward situation hibernate will map ech class to its own table.

How do we map Studnet.java, Addresss.java both with single mapping file correspondng to single table . please advise
0
 
for_yanCommented:
This is a video tutorial of how to map two classes to one table - takes only five minutes and at least the beginning
seems understanadable:
http://golearnweb.com/web-design-blog/java-hibernate-tutorial-part-12-two-classes-to-one-table-video.html
0
 
gudii9Author Commented:
i will watch
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.