Hibernate双向多对多实现 [英] Hibernate Bidirectional Many to Many implementation

查看:120
本文介绍了Hibernate双向多对多实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是hibernate的新手,我想在hibernate中实现这样的东西。为了能够这样做,我在设置xml映射文件时遇到问题。如果有人可以帮助我,这将是非常好的,因为这只是概念的证明我想做,我有很多复杂的事情要做。谢谢

  public class课程实现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>();
}

但在数据库中,我想要创建3个表
学生,课程和学生课程

 学生-----> studentCourse <------课程
StudentId StudentId CourseId
CourseId

我想要的是,当我做

 课程C1 =(课程)session.get(Course.class,CourseId)

我得到指定的课程,并通过

  Set< Student> ; StudentsEnrolled = C1.getStu(); 

我获得所有参加该课程的学生



Similary
当我做

 学生S1 =(学生)session.get(Student.class,StudentId) 

我得到指定的学生,并通过

 设置< Course> CoursesEnrolled = S1.getCourses(); 

我获得指定学生参加的所有课程

解决方案

查看 this

 < hibernate-mapping> 
< class name =com.vaannila.student.Studenttable =STUDENT>
< meta attribute =class-description>此类包含学生详细信息。< / meta>
< id name =studentIdtype =longcolumn =STUDENT_ID>
< generator class =native/>
< / id>
< property name =studentNametype =stringlength =100not-null =truecolumn =STUDENT_NAME/>
< set name =coursestable =STUDENT_COURSEcascade =all>
< key column =STUDENT_ID/>
< many-to-many column =COURSE_IDclass =com.vaannila.student.Course/>
< / set>
< / class>
< / hibernate-mapping>

< hibernate-mapping>
< class name =com.vaannila.student.Coursetable =COURSE>
< meta attribute =class-description>此课程包含课程详情。< / meta>
< id name =courseIdtype =longcolumn =COURSE_ID>
< generator class =native/>
< / id>
< property name =courseNametype =stringcolumn =COURSE_NAME/>
< set name =studentstable =STUDENT_COURSEcascade =all>
< key column =COURSE_ID/>
< many-to-many column =STUDENT_IDclass =com.vaannila.student.Student/>
< / set>
< / class>
< / hibernate-mapping>

好吧,如果你镜像在其他类这样的集合不工作? >

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 specified 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?

这篇关于Hibernate双向多对多实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆