Solved

component mapping in hibernate

Posted on 2011-03-24
7
311 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:gudii9
  • 3
  • 2
  • 2
7 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 35212203


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
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 250 total points
ID: 35213073
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
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35213082


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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 35353456






>>>

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
 
LVL 7

Author Comment

by:gudii9
ID: 35789961

>>>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
 
LVL 47

Expert Comment

by:for_yan
ID: 35790115
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
 
LVL 7

Author Comment

by:gudii9
ID: 35935914
i will watch
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
servlet web applications   metadata-complete="true" or false 3 187
maven module vs maven project 3 68
ejb entity bean example 2 31
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question