Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

1 to many case : howto ?

 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, anyone can guide how to make implement 1 to many case in hibernate ?

say i have 2 table


how to i make 1 to many relation and query for this case ...thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends - do you want the association to be unidirectional or bidirectional?

The most basic approach, if a Student has many Subjects, would be to define a many-to-one association in the Subject mapping to Student. Thats unidirectional. If you wanted to make it bidirectional you would also add some sort of appropriate Collection mapping in Student, which has a one-to-many association with Subject.

Make sense?
[ April 22, 2005: Message edited by: Paul Sturrock ]
 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for explanation , Paul Sturrock

but how about in coding n mapping ?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not in the spirit of this site to do people's work for them, so I won't post you an example. But I will try to explain again what you could do.

The way you have defined your two objects Subject looks like a dependent object - i.e. one doesn't exist without a Student (unless student_id is nullable). So your Student might have a property called 'subjects' which could be a Set. You would add a <set /> element to the Student.hbm.xml file with a one-to-many mapping to your associated class (and this is explained in the documentation). Your Subject class would have a property called 'student' which would be of the type Student. In the Subject.hbm.xml mapping file you would have the other side of the association, which you define as a many-to-one association the Student class. Again how to do that is in the documentation.

Try it. Feel free to post any problems you have/exceptions you encounter.
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't do this. If you need a relation just implement one more query in the data access layer. Usage of this complex DTO that have dependencies has big disadvantages:
1. Automatic DTO regeneration is difficult and sometime is not possible.
2. If you latter decide to jump from H to something else then you should rewrite you DTO because another framework probably do not support relation mapping.
3. If you load related objects together with a main one then you slowdown the load cycle. If you use lazy load than sometime collision between different collections could happen. I have got such cases and completely get rid of all relations in my DTO. Using of queries require a little more work but much safe.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic