I am new to hibernate and I want to implement something like this in hibernate. To be able to do this, I am getting problem in setting the xml mapping file. If someone can help me, it would be very nice as this is just of proof of concept I am trying to do, I have much complicated things to do.Thanks
public class Course implements java.io.Serializable
{
private long courseId;
private String courseName;
private Set <Student> Stu = new HashSet <Student>();
}
public class Student implements java.io.Serializable
{
private long studentId;
private String studentName;
private Set<Course> courses = new HashSet<Course>();
}
But in the database, I want 3 table to be created Student,Course and StudentCourse
Student----->StudentCourse<------Course
StudentId StudentId CourseId
CourseId
What I want is that when I do
Course C1=(Course)session.get(Course.class,CourseId)
I get the specified course and by doing
Set <Student> StudentsEnrolled=C1.getStu();
I get all students enrolled in that course
Similary When I do
Student S1=(Student)session.get(Student.class,StudentId)
I get the specified student and by doing
Set <Course> CoursesEnrolled=S1.getCourses();
I get all courses the s开发者_JS百科pecified student has taken
Take a look at this
<hibernate-mapping>
<class name="com.vaannila.student.Student" table="STUDENT">
<meta attribute="class-description">This class contains student details.</meta>
<id name="studentId" type="long" column="STUDENT_ID">
<generator class="native" />
</id>
<property name="studentName" type="string" length="100" not-null="true" column="STUDENT_NAME" />
<set name="courses" table="STUDENT_COURSE" cascade="all">
<key column="STUDENT_ID" />
<many-to-many column="COURSE_ID" class="com.vaannila.student.Course" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.vaannila.student.Course" table="COURSE">
<meta attribute="class-description">This class contains course details.</meta>
<id name="courseId" type="long" column="COURSE_ID">
<generator class="native"/>
</id>
<property name="courseName" type="string" column="COURSE_NAME"/>
<set name="students" table="STUDENT_COURSE" cascade="all">
<key column="COURSE_ID" />
<many-to-many column="STUDENT_ID" class="com.vaannila.student.Student" />
</set>
</class>
</hibernate-mapping>
Well if you mirror the set in the other class like this doesn't it work?
What you are looking for is very similar with the example stated here: http://technicalmumbojumbo.wordpress.com/2007/09/25/investigating-hibernate-associations-many-to-many/
It starts with a unidirectional relationship, and at the end it defines the mappings for the bidirectional one.
Hope this helps,
Octav
=====================================
Don't forget you have to decide which entity is the owner of this relationship: Student or Course.
Once you've decided that, the Hibernate mapping of the "owned" entity needs to have the "inverse" flag set to true.
That's exactly how it's described in the link above, if you check the Hibernate mapping for the Phone entity, at the end of the article.
精彩评论