Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

1 to many case : howto ?

 
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for explanation , Paul Sturrock

but how about in coding n mapping ?
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 120
IntelliJ IDE Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There's a way to do it better - find it. -Edison. A better tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic