Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


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)

Avatar of gudii9

ASKER







>>>

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
Avatar of gudii9

ASKER


>>>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
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
Avatar of gudii9

ASKER

i will watch