• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

one-to-many mapping...

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear madam/sir,

I hava to classes in my project (Student and Couse) as follows,

public class Student {
int id ;
java.util.List courses ;

//setter and getter methods
}

public class Course {

int id;
String name ;

//setter and getter methods
}

and I used to hibernate mapping to map them to db as follows

=======================================================
<hibernate-mapping>
<class name="Student" table="STUDENT">

<id name="id" type="int" column="id">
<generator class="increment"/>
</id>
<bag name="courses" cascade="all" >
<key column="student_id"/>
<one-to-many class="Course"/>
</bag>

</class>
</hibernate-mapping>
=======================================================
<hibernate-mapping>
<class name="Course" table="COURSE">

<id name="id" type="int" column="id">
<generator class="increment"/>
</id>

<property name="name" column="NAME" type="string"/>
</class>
</hibernate-mapping>

==================================================================
when I save a Student object with it's associations, it and it's associations do not save properly, student_id column in COURSE table, has not any value, so in retrieving Student, it's courses do not load !!

but if I add a many-to-one element in course mapping file, everything woulde be ok then,

My PROBLEM is, I want to have unidirectional mapping, just from student to course, not reverse.

what can I do then, to solve?

regards
AliReza
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding the inverse property to the bag as follows:

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"AliReza Ranch"

Please click on the My Profile link above and change your display name to meet the JavaRanch Naming Policy of using your real first and real last names.

Thanks

Mark
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic