?
Solved

hibernate mappings

Posted on 2010-01-08
15
Medium Priority
?
487 Views
Last Modified: 2013-11-24
Hi,

I am little bit confused about hibernate mappings.

Let us say i am dealing with one to many scenario where for example
 'student can have any number of phone numbers'
(consider we have corresponding student, phone,student_phone tables in database.

In this case how do i define it in the student.hbm.xml file?
If i define like

<?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" length="100" column="STUDENT_NAME" />
12.
<set name="studentPhoneNumbers" table="STUDENT_PHONE" cascade="all">
13.
<key column="STUDENT_ID" />
14.
<many-to-many column="PHONE_ID" unique="true" class="com.vaannila.student.Phone" />
15.
</set>
16.
</class>
17.
</hibernate-mapping>

Is that i is correct?

How to define in case of oneone, many-to-one.
Also how we define if we use annotations since there wont be any mapping files in that case.
Any ideas, suggestions, sample code,resources highly appreciated. thanks in advance
0
Comment
Question by:gudii9
  • 9
  • 5
15 Comments
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 400 total points
ID: 26261390
0
 
LVL 20

Accepted Solution

by:
Sathish David  Kumar N earned 1600 total points
ID: 26266477
For one-to-one

<one-to-one name="stundentId" property-ref="studentDetail" cascade = "save-update"/>
       
Here ur defined student id  will in both studentdetail table and ur table . both will enter the value when u r calling ur hbm file !!

<many-to-one name="studentPh" class="stunentPhBean" unique="true" cascade="save-update">
                        <column name="student_Ph" />
                    </many-to-one>
stunentPhBean is the POJO class that will update or save when ur save or update the ur hbm file !!  
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26266544
Annotataion is little different u dont need to go hbm file !!
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 20

Expert Comment

by:Sathish David Kumar N
ID: 26266874

 
@Id
@JoinColumn(name = "student_id")
@OneToOne(cascade = {}, fetch = FetchType.EAGER, optional = false, targetEntity = studentDetail.class)
private StudentId studentId;
}
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26267054
@Entity()
public class Flight implements Serializable {
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinColumn(name="COMP_ID")
    public Company getCompany() {
        return company;
    }
    ...
}
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26267074
0
 
LVL 7

Author Comment

by:gudii9
ID: 26272842
Hi,

I still did not get it clearly what we are trying to say in case of one-many


<many-to-many column="PHONE_ID" unique="true" class="com.vaannila.student.Phone" />


Also

>>>
Here ur defined student id  will in both studentdetail table and ur table


Please advise
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26272962
can u tell me clearly ur debout ??

cascade="save-update"  u will mention in ur relation many-to-many

this will take care of ur 2 tables store !!

one is ur
com.vaannila.student.Student, other one is com.vaannila.student.Phone  both will save or update !!

0
 
LVL 7

Author Comment

by:gudii9
ID: 26278472
Hi,

I read as

>>>We use many-to-many element to create the one-to-many relationship between the Student and Phone entities. Since a student can have any number of phone numbers we use a collection to hold the values. In this case we use Set. Many-to-many element is usually used to create many-to-many relationship, here we place the unique constraint on the PHONE_ID column, this makes the relationship one-to-many.


I could not get why we need to use 'unique etc constraints.

>>>><many-to-many column="PHONE_ID" unique="true" class="com.vaannila.student.Phone" />

please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 26278556
Hi,

Why we gave unique constraint on PHONE_ID but not on STUDENT_ID?

>>>>
<key column="STUDENT_ID" />
<many-to-many column="PHONE_ID" unique="true" class="com.vaannila.student.Phone" />

 I thought since one student can have multiple phone numbers like mobile, home etc unique has to be given on STUDENT_ID. Why author doing in opposite way. Please advise


Also one-one i see unique constraint is applied on student_address column like

>>><many-to-one name="studentAddress" class="com.vaannila.student.Address" column="STUDENT_ADDRESS" not-null="true" cascade="all" unique="true" />

</class>
</hibernate-mapping>


We use the many-to-one element to create the one-to-one relationship between the Student and Address entities. We do this my making the STUDENT_ADDRESS column unique in the STUDENT table.



Please advise
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26280831
<<<<<I could not get why we need to use 'unique etc constraints.

unique  is not a mantory one !!
as per this code !! ph no should be unique for each person  thats y we gave that !!

cascade="save-update"
this for ur telling the relationship between parent and child !!
if parent will update means child also updated !! that purpose we mention this !!


class="com.vaannila.student.Phone"
this for child or parent table def !! as per this code !!
student is parent class and child class parent is phone !!
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26280849
<<<<<<<I thought since one student can have multiple phone numbers like mobile, home etc unique has to be given on STUDENT_ID. Why author doing in opposite way. Please advise

ya ur correct the person ! a single person have multiple ph no

but no should be unique  right !!

eg:
i have 2 no
012345678
98765432
both the no are diffrent right that y we give unique, we dont want to give same no again !!
for that person each no is unique  (no repeat no for that person)
0
 
LVL 7

Author Comment

by:gudii9
ID: 26285222
Hi,

Unique make more sense to me now. The cascade='all' and cascade='save and update' etc does not make sense yet. please advise
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26285352
cascade is used for enable the operation of child table ....

if u wiill enter some data like phone_no that will save in 2 tables student,phone table .
phone_no only connect the 2 tables ...  right !! u need not enter 2 times ph_no
onces u enter that will save the both tables !!

what opertaion u will perform that purpose u  give cascade

if u cascade="save-update"
parent and child table will update same time !!  

if u give cascade="all"
all operation del,save,update !!
0
 
LVL 7

Author Closing Comment

by:gudii9
ID: 31674911
thank you very much
0

Featured Post

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!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 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