解决Hibernate删除异常:deleted object would be re-saved by cascade

我们在指定了数据库表的关联映射后,在删除其中一方的的时候,提示了以下异常:deleted object would be re-saved by cascade (remove deleted object from associations): [entity.Associationinformation#7]

(注:entity.Associationinformation是实体类,7为设定的外键的值)


下面是我的多对一关联情况:

1、实体类:

entity.Associationinformation和entity.Associationactivitylist,前者为one,后者为many。

Associationinformation.java:

public class Associationinformation implements java.io.Serializable {
private Integer assoId;
private String assoName;
private String assoPosition;
private Integer assoPersonNum;
private String assoBuildDate;
private String assoIntroduce;
private String assoServiceObject;
//一对一的社团负责人
private Associationrespeople assoResID;
//一对多的社团活动
private Set<Associationactivitylist> assoActivity;
//省略get/set方法
}

Associationactivitylist.java:

public class Associationactivitylist implements java.io.Serializable {
private Integer activityId;
private String activityName;
private String activityPosition;
private Integer activityPersonNum;
private String activityTime;
private String activityContent;
private Associationinformation association;
//省略get/set方法
}

2、映射文件:*.hbm.xml

Associationinformation.hbm.xml:

<hibernate-mapping>
    <class name="entity.Associationinformation" table="associationinformation" catalog="stu_association">
        <id name="assoId" type="java.lang.Integer">
            <column name="AssoID" />
            <generator class="increment" />
        </id>
        <property name="assoName" type="java.lang.String">
            <column name="AssoName" length="20" />
        </property>
        <property name="assoPosition" type="java.lang.String">
            <column name="AssoPosition" length="20" />
        </property>
        <property name="assoPersonNum" type="java.lang.Integer">
            <column name="AssoPersonNum" />
        </property>
        <property name="assoBuildDate" type="java.lang.String">
            <column name="AssoBuildDate" length="10" />
        </property>
      <!--  
        <property name="assoResName" type="java.lang.String">
            <column name="AssoResName" length="20" />
        </property>-->
        <property name="assoIntroduce" type="java.lang.String">
            <column name="AssoIntroduce" length="150" />
        </property>
        <property name="assoServiceObject" type="java.lang.String">
            <column name="AssoServiceObject" length="20" />
        </property>
        <!-- 一对多的社团与社团活动 -->
         <set name="assoActivity" table="associationactivitylist">
            <key column="assoID"></key>
            <one-to-many class="entity.Associationactivitylist"/>
        </set>
        <!-- 一对一的社团负责人 -->
        <many-to-one name="assoResID"
            class="entity.Associationrespeople"
            column="assoResID"
            lazy="false"
            cascade="all"
            unique="true"
        ></many-to-one>
   </class>
</hibernate-mapping>

Associationactivitylist.hbm.xml:

<hibernate-mapping>
    <class name="entity.Associationactivitylist" table="associationactivitylist" catalog="stu_association">
        <id name="activityId" type="java.lang.Integer">
            <column name="ActivityID" />
            <generator class="increment" />
        </id>
        <property name="activityName" type="java.lang.String">
            <column name="ActivityName" length="20" not-null="true" />
        </property>
        <property name="activityPosition" type="java.lang.String">
            <column name="ActivityPosition" length="20" not-null="true" />
        </property>
        <property name="activityPersonNum" type="java.lang.Integer">
            <column name="ActivityPersonNum" />
        </property>
        <property name="activityTime" type="java.lang.String">
            <column name="ActivityTime" length="10" not-null="true" />
        </property>
        <property name="activityContent" type="java.lang.String">
            <column name="ActivityContent" length="150" />
        </property>
        <!-- 配置多对一关联关系 -->
<many-to-one name="association" class="entity.Associationinformation" column="assoID" cascade="all">
</many-to-one>
    </class>
</hibernate-mapping>

记得在Hibernate.cfg.xml中声明映射文件的路径:

<mapping resource="entity/Associationinformation.hbm.xml" />
<mapping resource="entity/Associationactivitylist.hbm.xml" />

到这里就完成了一对多的关联配置,然后在Action中写Delete()方法:

public String deleteActivity(){
...
Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
assoActivityDao.delete(associationactivitylist);
tx.commit();
HibernateSessionFactory.closeSession();
...
}

请注意,这样写的话,就会报出以上异常,如果没有关联关系,这样写是可以的。结合了网上的方法,解决方法如下:

1、删除Set方的cascade:(缺点:子对象和父对象不能进行级联更新,没意义)

2、解决关联关系后,再删除(个人推荐):

将Action中的方法改为:

public String deleteActivity(){
...
Associationactivitylist associationactivitylist = assoActivityDao.findById(AssoID);
Associationinformation Association = associationactivitylist.getAssociation();
Association.getAssoActivity().remove(associationactivitylist);
associationactivitylist.setAssociation(null);
Transaction tx = HibernateSessionFactory.getSession().beginTransaction();
assoActivityDao.delete(associationactivitylist);
tx.commit();
HibernateSessionFactory.closeSession();
...
}

分别获取到社团和社团活动对象后,利用remove方法解除关联管理,再将one方的值设为null。



你可能感兴趣的:(Hibernate,object,by,cascade,deleted,be,re-saved,would,删除关联)