Solved

component mapping in hibernate

Posted on 2011-03-24
7
283 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
Comment Utility


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
Comment Utility
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
Comment Utility


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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 7

Author Comment

by:gudii9
Comment Utility






>>>

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
Comment Utility

>>>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
Comment Utility
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
Comment Utility
i will watch
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Books that can get me started on JAVA 2 52
strCopies  challenge 17 73
wordappend challenge 8 83
wordmultiple challenge 12 89
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now