1

我只是开始学习 Hibernate 并面临这样的任务。我有 3 个表:students(id,name), courses(id,name), 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>

连接是多对多的 除了不创建4个表之外,如何将这3个表连接在一起?我在想我在创建多对多连接时犯了什么错误。请帮我。

4

0 回答 0