• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Mapping

 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

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
 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The mapping file that you have written is for many to one because of the unique constraint that you have mentioned. If you remove the unique constraint or set the value to false it is many to many.

In annotations based approach you would be defining the annotations directly inside the class which needs to be persisted. The hbm2ddl will scan through the classes and do the rest for you. The syntax is merely available. In mu opinion you should give a try using annotations.
 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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




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



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



 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its the other way round. One student can have many phone numbers like home, work, mobile etc> The details will be stored in the PHONE table. STUDENT_ID is the foreign key in the Phone table refering to the primary key of Student table. By making Student id column unique a student can have only one phone number. Otherwhise if we make the PhoneId a unique constraint it solves the problem and a student can have many unique phone numbers. If you miss out on unique constraint on the phoneid many student can have the same phone numbers. Hope this solves your query :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic