hibernate:如何在没有JPA注释的情况下将连接表映射到额外的列

时间:2012-10-22 16:11:10

标签: hibernate list hibernate-mapping

假设我有以下课程:

class Student{
  String name;
  List<StudentClassMark> courseMarks;
}

class Class {
  String title;
}

class StudentClassMark {
   Student student;
   Class class;
   MarkType markType;   <-- notice this is the extra column that is causing problems
}

class MarkType {
    String description;
}

如何使用hibernate映射文件映射列表(所以没有JPA注释)?这是我试过的:

<class name="Student" table="PERSON">
    <property name="name" column="NAME"/>
    <bag name="courseMarks" inverse="false" cascade="all" table="STUDENT_CLASS_MARK">
        <key column="STUDENT" />
        <many-to-many>    <-- this is the section that I don't know how to do
            <column name="CLASS"/>
            <column name="MARK_TYPE"/>
        </many-to-many>
    </bag>
</class>

我发现的例子都使用了我无法使用的JPA表示法。

由于

1 个答案:

答案 0 :(得分:1)

好的,所以我明白了。这是正确的映射(我希望它可以帮助其他人):

<class name="Student" table="PERSON">
    <property name="name" column="NAME"/>
    <bag name="courseMarks" inverse="false" cascade="all" table="STUDENT_CLASS_MARK">
        <key column="STUDENT" />
         <composite-element class="StudentClassMark">
             <many-to-one name="class" column="CLASS" />
             <many-to-one name="markType" column="MARK_TYPE" />
         </composite-element>
    </bag>
</class>

<class name="StudentClassMark" table="StudentClassMark">
   <property name="description" column="DESCRIPTION"/>
</class>