Hibernate双向多对多实现

发布于 2024-10-05 23:59:32 字数 1213 浏览 1 评论 0原文

我是休眠新手,我想在休眠中实现类似的东西。为了能够做到这一点,我在设置 xml 映射文件时遇到了问题。如果有人可以帮助我,那就太好了,因为这只是我想做的概念证明,我有很多复杂的事情要做。谢谢,

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>();
}

但是在数据库中,我想创建 3 个表 Student,Course 和 StudentCourse

              Student----->StudentCourse<------Course 
            StudentId      StudentId          CourseId     
                            CourseId     

我想要的是,当我这样做时,

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

我会得到指定的课程,并且通过这样做,

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

我会让所有学生都注册该

课程 当我这样做时,

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

我会得到指定的学生,并且通过这样做,

Set <Course> CoursesEnrolled=S1.getCourses();

我会得到指定学生已修读的所有课程

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-10-12 23:59:32

看看这个

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

好吧,如果你像这样镜像另一个类中的集合,这不起作用吗?

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?

楠木可依 2024-10-12 23:59:32

您正在寻找的内容与此处所述的示例非常相似:
http://technicalmumbojumbo.wordpress.com/ 2007/09/25/investigating-hibernate-associations-many-to-many/

它从单向关系开始,最后定义双向关系的映射。

希望这有帮助,
奥克塔夫

=======================================

不要忘记您必须决定哪个实体是此关系的所有者:学生或课程。
一旦您决定了这一点,“拥有”实体的 Hibernate 映射需要将“inverse”标志设置为 true。
如果您在文章末尾检查 Phone 实体的 Hibernate 映射,这正是上面链接中描述的方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文