Hibernate:如何连接三个表(使用映射)

时间:2012-12-06 18:35:45

标签: hibernate mapping many-to-many

我只是开始学习Hibernate并面对这样的任务。 我有3个表:学生(id,姓名),课程(id,姓名),mark(id_student,id_course,mark)。

Courses.java
public class Courses implements Serializable {
    private Integer id;
    private String  courseTitle;
    private Set<Marks> mark = new HashSet<Marks>
getters and setters...

Students.java
public class Students implements Serializable {
    private Integer id;
    private String  name;
    private Set<Marks> students = new HashSet<Marks>;
getters and setters...

Marks.java
public class Marks implements Serializable {
    private Integer courseId;
    private Integer studentId;
    private Integer mark;
    private Set<Students> marks = new HashSet<Students>;
getters and setters...

Students.hbm.xml
<class name="Students" table="students">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="name" column="NAME"/>
        <set name="students" inverse="true" table="marks">
            <key column="NUM" />
            <many-to-many column="STUDENT_ID" class="Marks" />
        </set>
    </class>

Courses.hbm.xml
 <class name="Courses" table="courses">
        <id name="id" column="ID" type="integer">
            <generator class="native"/>
        </id>
        <property name="courseTitle" column="COURSE_TITLE"/>
        <set name="mark" inverse="true" table="marks">
            <key column="NUMBER" />
            <many-to-many column="COURSE_ID" class="Marks" />
        </set>
    </class>

Marks.hbm.xm
 <class name="Marks" table="marks">
        <id name="studentId" column="STUDENT_ID">
            <generator class="native"/>
        </id>
        <property name="courseId" column="COURSE_ID"/>
        <property name="mark" column="MARK" lazy="true"/>
    </class>

连接是多对多的 如何将这3张桌子连接在一起,除了不创建4桌子? 在创建多对多连接时,我正在思考我犯了什么错误。     请帮帮我。

0 个答案:

没有答案