I assume that Student and Grades are defined as two separate tables? I also assume that a student can have more than one grade. I also assume that the CourseName is part of the ID of your Grade record; as your example implies.
If so you can have hibernate use the object graph on the heap to determine the relationships between your objects; and set the appropriate foreign-keys on save.
You can do this by specifying a key-many-to-one relationship in your Grade mapping.
ex:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="myapp.dco">
<class name="Grade" table="Grade">
<composite-id name="key" class="GradePk">
<meta attribute="use-in-equals">true</meta>
<key-many-to-one column="StudentID" name="student" class="myapp.dco.Student" />
<key-property column="CourseName" name="courseName" type="string" />
</composite-id>
<property column="LetterGrade" name="letterGrade" type="string" />
</class>
</hibernate-mapping>
Next, modify your grade's key object (GradePK) to hold a reference to a Student.
ex
GradePk.java
Code:
public class GradePk{
private Student student;
private String courseName;
...
}
Grade.java
Code:
public class Grade{
private GradePk key;
private String letterGrade;
...
public void setStudent( Student student ){
key.setStudent( student );
}
Finally, you'll want to add a mechanism to associate the objects by reference when adding Grades to your Student's grade set:
Code:
public class Student{
private Set<Grade> gradeSet;
....
public void addGrade( Grade grade ){
// First associate the grade with the current user
grade.setStudent( this );
// Add the grade to the student's gradeSet
gradeSet.add( grade );
}
}
Make sure to map the grades collection on the Student.hbm.xml and set cascade="all-delete-orphan".
Since the relationship between Grade and Student is mapped by object reference:
Quote:
<key-many-to-one column="Student" name="student" class="myapp.dco.Student" /> )
and we set the object references up when we add grades:
Code:
public void addGrade( Grade grade ){
// First associate the grade with the current user
grade.setStudent( this );
gradeSet.add( grade );
}
when hibernate saves the Student object and cascades the save operation to the set of grade objects Hibernate is smart enough to realize that the objects in the gradeSet are children of the student record and executes inserts in the appropriate order; IE: Insert Student; foreach grade insert grade. Better yet, it also assigns the parent student's ID into the studentID foreign-key column of the Grade object automatically.
You might want to check some of the documentation; as it's been a while since I've actually done this. Hopefully this is enough to get you started.
Cheers,
Aaron