您的位置:程序门 -> java -> j2ee / ejb / jms



hibernate级联删除奇怪问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


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>


请高手帮助看看,我真的不知道出在哪里,,很奇怪。。
发表于:2007-09-30 10:10:231楼 得分:0
favorite

这个表和article   有关联
发表于:2007-09-30 17:36:412楼 得分:0
问题已解决了,
就是在user.hbm.xml与article.hbm.xml中将相对应的多对多关联都设为   inverse="true"
就可以了,虽然不知道为什么。但就是行了。。


快速检索

最新资讯
热门点击