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

one-to-many mapping...

 
AliReza Naserzadeh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding the inverse property to the bag as follows:

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic