我有一个大学 table 和一个流 Table。 College 与 Stream table 具有一对多关系。现在,如果我删除一所大学,它会抛出一个错误
I have a College table and a Stream Table. College has one to many relationship with Stream table. Now if i delete a College it throws an error
在 Hibernate 中,我有 2 个 tables 即 College 和 Stream,因此有 2 个 POJO classes。现在 College 与 Stream 有多对多的关系,这是通过在 POJO class College 中创建一个 Set<Stream> streams = new HashSet<Stream>()
来实现的。所有必需的映射都在 College.hbm.xml
文件中完成。
现在,当我从学院 table 中删除一所学院时,它会抛出一个错误:update or delete on table "stream" violates foreign key constraint "fk1rk5ykysbvn2uddkm345fi00y" on table "college_stream".
我在休眠映射文件中使用了 cascade="all"
但同样的错误。我能做些什么??
<id name="id" column="id"/>
<property name = "address" column="address"/>
<property name = "country" column="country"/>
<property name = "grants" column="grants"/>
<property name = "name" column="name"/>
<set name="streams" table="COLLEGE_STREAM" cascade="all">
<key column="college_id" />
<many-to-many column="stream_Id" class="com.paramatrix.bean.Stream" />
</set>
这里发生的事情是您在 Stream 对象上设置了 Cascade All。所以它试图删除流。您需要在 College 对象上设置此集合,并由 CollegeStream 映射。我会试试这个
首先修改学院并从中删除 Set<Stream>
。接下来创建扩展 College 并将它们映射到一起的 CollegeStream:
public CollegeStream extends College {
private College college;
private Set<Stream> streams;
// Getters/Setters
}
然后使用 Hibernate 将 CollegeStream 映射到 College/Stream,将 Cascade 添加到 College 映射
<class name="CollegeStream" table="COLLEGE_STREAM">
<many-to-one name="college" class="com.paramatrix.bean.College" cascade="all">
<column name="college_id />
</many-to-one>
<set name="streams" table="STREAM">
<key column="stream_id" />
<many-to-many class="com.paramatrix.bean.Stream" />
</set>
</class>
这样,当您加载 CollegeStream 对象时,它将包含来自与流集映射的学院的数据。当您删除 CollegeStream 时,它应该会删除链接的 College 以及 CollegeStream 而不会删除 Stream。
在 Hibernate 中,我有 2 个 tables 即 College 和 Stream,因此有 2 个 POJO classes。现在 College 与 Stream 有多对多的关系,这是通过在 POJO class College 中创建一个 Set<Stream> streams = new HashSet<Stream>()
来实现的。所有必需的映射都在 College.hbm.xml
文件中完成。
现在,当我从学院 table 中删除一所学院时,它会抛出一个错误:update or delete on table "stream" violates foreign key constraint "fk1rk5ykysbvn2uddkm345fi00y" on table "college_stream".
我在休眠映射文件中使用了 cascade="all"
但同样的错误。我能做些什么??
<id name="id" column="id"/>
<property name = "address" column="address"/>
<property name = "country" column="country"/>
<property name = "grants" column="grants"/>
<property name = "name" column="name"/>
<set name="streams" table="COLLEGE_STREAM" cascade="all">
<key column="college_id" />
<many-to-many column="stream_Id" class="com.paramatrix.bean.Stream" />
</set>
这里发生的事情是您在 Stream 对象上设置了 Cascade All。所以它试图删除流。您需要在 College 对象上设置此集合,并由 CollegeStream 映射。我会试试这个
首先修改学院并从中删除 Set<Stream>
。接下来创建扩展 College 并将它们映射到一起的 CollegeStream:
public CollegeStream extends College {
private College college;
private Set<Stream> streams;
// Getters/Setters
}
然后使用 Hibernate 将 CollegeStream 映射到 College/Stream,将 Cascade 添加到 College 映射
<class name="CollegeStream" table="COLLEGE_STREAM">
<many-to-one name="college" class="com.paramatrix.bean.College" cascade="all">
<column name="college_id />
</many-to-one>
<set name="streams" table="STREAM">
<key column="stream_id" />
<many-to-many class="com.paramatrix.bean.Stream" />
</set>
</class>
这样,当您加载 CollegeStream 对象时,它将包含来自与流集映射的学院的数据。当您删除 CollegeStream 时,它应该会删除链接的 College 以及 CollegeStream 而不会删除 Stream。