Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 更新多对多属性_Hibernate_Many To Many_Hibernate Mapping_Hbm - Fatal编程技术网

Hibernate 更新多对多属性

Hibernate 更新多对多属性,hibernate,many-to-many,hibernate-mapping,hbm,Hibernate,Many To Many,Hibernate Mapping,Hbm,我在学生表和课程表之间有一个多对多的关联,通过StudentCourse连接起来。 StudentCourse有一个额外的属性称为grade。 为了让学生能够访问特定课程的成绩,我在student.hbm.xml中编写了以下映射: <map name="coursesGrades" table="StudentCourse" lazy="false" cascade="save-update" inverse="true"> <key column="studentId

我在学生表和课程表之间有一个多对多的关联,通过StudentCourse连接起来。 StudentCourse有一个额外的属性称为grade。 为了让学生能够访问特定课程的成绩,我在student.hbm.xml中编写了以下映射:

<map name="coursesGrades" table="StudentCourse" lazy="false" cascade="save-update" inverse="true">
    <key column="studentId" />
    <map-key-many-to-many class="Course" column="courseId" />
    <element type="integer" column="grade" />
</map>

获取数据时,一切似乎都很好。但是,当尝试更新成绩时,我会遇到一个违反了唯一约束的异常,因为hibernate没有更新行,而是尝试插入一个具有相同studentId和courseId(定义为唯一)的新行

所以我的问题是:如何让hibernate根据studentId和courseId更新地图?

StudentCourse.hbm.xml如下所示:

<class name="StudentCourse" table="StudentCourse"...>       
        <composite-id name="pk" class="StudentCoursePK">
            <key-many-to-one name="Student" column="studentId"/>
            <key-many-to-one name="Course" column="courseId"/>
        </composite-id>
        <property name="grade" type="integer"/>         
</class>


任何帮助都将得到感谢。

由于您将课程成绩地图的“反向”设置为true,这意味着学生不是此关系的所有者,因此它不会更新成绩。如何准确地更新数据?另外,我不理解您的学生课程类映射,学生中定义的映射是否足以映射学生课程表

好的,我知道了。我做的很好,但数据库中的数据有问题,StudentCourse实际上引用了Student中的另一列,而不是studentId。修复了它,一切正常。不管怎样,希望这能帮助某人…忽略评论。仍然不起作用:(