Hibernate级联删除奇怪问题

justin_wd 2007-09-30 09:45:44
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="model.ArticleType" table="articletype" catalog="javasubject" >
<id name="arttId" type="long">
<column name="artt_id" />
<generator class="native" />
</id>
<many-to-one name="parent" class="model.ArticleType" fetch="select" outer-join="true">
<column name="artt_up" /> <!-- 指向当前类型的父类型 -->
</many-to-one>
<property name="arttName" type="string">
<column name="artt_name" length="20" />
</property>
<property name="arttType" type="string">
<column name="artt_type" length="20" />
</property>
<set name="articles" inverse="true" cascade="all" >
<key>
<column name="artt_id" />
</key>
<one-to-many class="model.Article" /> <!-- 与article一对多关联 -->
</set>
<set name="children" inverse="true" cascade="all" lazy="false">
<key>
<column name="artt_up" />
</key> <!-- 指向当前类型的子类型 -->
<one-to-many class="model.ArticleType" />
</set>
</class>
</hibernate-mapping>


以上是我的ArticleType(文章分类)的hbm.xml文件,ArticleType与Article是一对多关联
我以前刚刚写好DAO层的时候,做过测试的,
删除ArticleType,是可以级联删除Article(文章),
但是,现在,整合了struts1.2以及spring2.0以后,既然删掉ArticleType时,如果有所下属的
Article.就会出现以下的提示错误:

Could not execute JDBC batch update; SQL [delete from javasubject.article where art_id=?]; Cannot delete or update a parent row: a foreign key constraint fails (`javasubject/favorite`, CONSTRAINT `FK_favorite2` FOREIGN KEY (`art_id`) REFERENCES `article` (`art_id`)); nested exception is java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`javasubject/favorite`, CONSTRAINT `FK_favorite2` FOREIGN KEY (`art_id`) REFERENCES `article` (`art_id`))


我的Article表与Review表,也是一对多关联,
删除Article时,却可以级联删除Review,以下是hbm.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="model.Article" table="article" catalog="javasubject">
<id name="artId" type="long">
<column name="art_id" />
<generator class="native" />
</id>
<many-to-one name="articletype" class="model.ArticleType" fetch="select" outer-join="true" >
<column name="artt_id" />
</many-to-one>
<property name="artTitle" type="string">
<column name="art_title" length="50" />
</property>
<property name="artAuthor" type="string">
<column name="art_author" length="20" />
</property>
<property name="artText" type="string">
<column name="art_text" length="65535" />
</property>
<property name="artSource" type="string">
<column name="art_source" length="50" />
</property>
<property name="artType" type="string">
<column name="art_type" length="20" />
</property>
<property name="artTime" type="timestamp">
<column name="art_time" length="0" not-null="true" />
</property>
<set name="reviews" inverse="true" cascade="all">
<key>
<column name="art_id" />
</key>
<one-to-many class="model.Review" />
</set>

<set name="keyWords" inverse="true" cascade="all">
<key>
<column name="art_id" />
</key>
<one-to-many class="model.KeyWord" />
</set>

<set name="users" inverse="true" table="favorite">
<key>
<column name="art_id" not-null="true" />
</key>
<many-to-many class="model.User" column="user_id" outer-join="auto"/>
</set>
</class>
</hibernate-mapping>


请高手帮助看看,我真的不知道出在哪里,,很奇怪。。
...全文
617 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

我的也是啊,加上inverse="true"就可以了,为什么,有待研究
[/Quote]我的加上依然不行。。
maiee 2012-02-28
  • 打赏
  • 举报
回复
我的也是啊,加上inverse="true"就可以了,为什么,有待研究
shadao 2007-09-30
  • 打赏
  • 举报
回复
favorite

这个表和article 有关联
justin_wd 2007-09-30
  • 打赏
  • 举报
回复
问题已解决了,
就是在User.hbm.xml与Article.hbm.xml中将相对应的多对多关联都设为 inverse="true"
就可以了,虽然不知道为什么。但就是行了。。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧