cuiyi's blog(崔毅 crazycy)

记录点滴 鉴往事之得失 以资于发展
数据加载中……

Hibernate之deleted object would be re-saved by cascade异常

在Hibernate中,删除存在关联关系的一个对象时,会出现 org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)这个异常

如下:
持久化类:
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * 产品类别对象
 * 
@author crazycy
 *
 
*/
public class ProductCategory extends BaseObject {

    
// Fields    

    
private long id;

    
private String name;

    
private String description;

    
private ProductCategory parentCategory;

    
private Set childrenCategories = new HashSet();

    
// Constructors

    
/** default constructor */
    
public ProductCategory() {
    }

    
/** minimal constructor */
    
public ProductCategory(String name) {
        
this.name = name;
    }
    
    
public ProductCategory(String name, String description) {
        
this.name = name;
        
this.description = description;
    }

    
/** full constructor */
    
public ProductCategory(String name, String description,
            ProductCategory parentCategory) {
        
this.name = name;
        
this.description = description;
        
this.parentCategory = parentCategory;
    }

    
/** full constructor */
    
public ProductCategory(String name, String description,
            Set childrenCategories) {
        
this.name = name;
        
this.description = description;
        
this.childrenCategories = childrenCategories;
    }

    
// Property accessors

    
public long getId() {
        
return this.id;
    }

    
public void setId(long id) {
        
this.id = id;
    }

    
public String getName() {
        
return this.name;
    }

    
public void setName(String name) {
        
this.name = name;
    }

    
public String getDescription() {
        
return this.description;
    }

    
public void setDescription(String description) {
        
this.description = description;
    }

    
public ProductCategory getParentCategory() {
        
return this.parentCategory;
    }

    
public void setParentCategory(ProductCategory parentCategory) {
        
this.parentCategory = parentCategory;
    }
    
    
/**
     * 由主来调:是主添加
     * 
@param productCategory
     
*/
    
public void addCategory(ProductCategory productCategory) {
        productCategory.setParentCategory(
this);
        childrenCategories.add(productCategory);
    }
    
    
/**
     * 由主来调;是从主删除
     * 
@param productCategory
     
*/
    
public void removeCategory(ProductCategory productCategory) {
        childrenCategories.remove(productCategory);
        productCategory.setParentCategory(
null);
    }
    
    
public Set getChildrenCategories() {
        
return childrenCategories;
    }

    
public void setChildrenCategories(Set childrenCategories) {
        
this.childrenCategories = childrenCategories;
    }

    
/**
     * 
@see java.lang.Object#equals(Object)
     
*/
    
public boolean equals(Object object) {
        
if (!(object instanceof ProductCategory)) {
            
return false;
        }
        ProductCategory rhs 
= (ProductCategory) object;
        
return new EqualsBuilder().append(this.description, rhs.description)
                .append(
this.name, rhs.name).append(this.id, rhs.id).isEquals();
    }

    
/**
     * 
@see java.lang.Object#hashCode()
     
*/
    
public int hashCode() {
        
return new HashCodeBuilder(1009592109-669108101).append(
                
this.description).append(this.name).append(this.id)
                .toHashCode();
    }

    
/**
     * 
@see java.lang.Object#toString()
     
*/
    
public String toString() {
        
return new ToStringBuilder(this).append("name"this.name).append(
                
"description"this.description).append("id"this.id)
                .toString();
    }

}

映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="">
    
<class name="ProductCategory" table="productcategory">
        
<id name="id" type="long">
            
<column name="ID" />
            
<generator class="native" />
        
</id>
        
<property name="name" type="string">
            
<column name="name" length="50" not-null="true" />
        
</property>
        
<property name="description" type="string">
            
<column name="description" length="150" />
        
</property>
        
<set name="childrenCategories" cascade="save-update" inverse="true">
            
<key column="parent"/>
            
<one-to-many class="ProductCategory"/>
        
</set>
        
<many-to-one name="parentCategory" column="parent" 
            
class="ProductCategory" 
            cascade
="save-update"
         
>
        
</many-to-one>
    
</class>
</hibernate-mapping>

测试代码:
category2.getChildrenCategories().remove(category5);
        category5.setParentCategory(
null);
        dao.removeProductCategory(category5.getId());

解决方案如下:
方法1 删除Set方的cascade
方法2 解决关联关系后,再删除 :
category2.getChildrenCategories().remove(category5);
        category5.setParentCategory(
null);
        dao.removeProductCategory(category5.getId());
方法3 在many-to-one方增加cascade 但值不能是none

如果以上三个方案都失败(哼哼~ 我用了5个小时才找出来的)
检查一下hashCode equals是否使用了id作为唯一标示的选项了;我用uuid.hex时是没有问题的;
但是用了native,就不行了,怎么办?删除啊!

也就是问题出现在本文给出的持久化类的hashCode equals方法身上


posted on 2006-06-24 22:07 crazycy 阅读(33635) 评论(18)  编辑  收藏

评论

# re: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)异常的解决  回复  更多评论   

标题太长了。
2006-06-24 22:44 | dudu

# re: Hibernate之deleted object would be re-saved by cascade 异常的解决  回复  更多评论   

嗯;谢谢;

及时截短了:)
原先标题:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)异常的解决
2006-06-24 22:50 | crazycy

# re: Hibernate之deleted object would be re-saved by cascade 异常的解决  回复  更多评论   

曾遇到与楼主同样问题,仅想到方法一方法二而已,佩服~
2006-07-06 08:45 | Y04069

# re: Hibernate之deleted object would be re-saved by cascade 异常的解决  回复  更多评论   

谢谢楼主,生Q^_^
偶正为这个问题头痛啊
2006-09-08 08:41 | coolbechy

# re: Hibernate之deleted object would be re-saved by cascade 异常的解决  回复  更多评论   

标题太长了。
2007-05-09 22:56 | 监听器

# re: Hibernate之deleted object would be re-saved by cascade 异常的解决  回复  更多评论   

经鸡巴回复没逼用的话 !

长你妈个逼

怎么决绝的问题 ? !

2007-07-24 12:20 | 万里

# re: Hibernate之deleted object would be re-saved by cascade 异常的解决  回复  更多评论   

http://www.blogjava.net/crazycy/archive/2006/07/07/57214.html


方法1 删除Set方的cascade.

方法2 解决关联关系后,再删除.

方法3 在many-to-one方增加cascade 但值不能是none

最后一招:
检查一下hashCode equals是否使用了id作为唯一标示的选项了;我用uuid.hex时是没有问题的;但是用了native,就不行了,怎么办?删除啊!

哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
2007-07-25 14:20 | 万里

# re: Hibernate之deleted object would be re-saved by cascade 异常的解决  回复  更多评论   

LZ 你太有才了 ! 谢了 !

问题解决完毕 .

推荐给大家一个新的架构:

* Struts 2 + Spring 2 + JPA + AJAX *

http://struts.apache.org/2.0.9/docs/struts-2-spring-2-jpa-ajax.html
2007-07-25 14:31 | 万里

# re: Hibernate之deleted object would be re-saved by cascade异常  回复  更多评论   

不错,学习了.
2007-10-26 20:44 | 中华信鸽

# re: Hibernate之deleted object would be re-saved by cascade异常  回复  更多评论   

试试先,应该可以,thank you.
2007-11-14 18:39 | suhaoyuan

# re: Hibernate之deleted object would be re-saved by cascade异常  回复  更多评论   

看上去似乎能解决我的问题 :)
2008-05-22 21:13 | camelwoo

# re: Hibernate之deleted object would be re-saved by cascade异常  回复  更多评论   

谢谢楼主
问题解决啦
2009-01-07 10:23 | 大是大非

# re: Hibernate之deleted object would be re-saved by cascade异常  回复  更多评论   

告诉你们一个简单的办法,你出现这个问题应该是你查询出来的语句的状态还在[associations] 你只需要用 this.getHibernateTemplate().clear();他就会把这条语句的状态改变,你就可以删除了。
2009-10-17 10:00 | - -

# re: Hibernate之deleted object would be re-saved by cascade异常[未登录]  回复  更多评论   

找方法应付,而没有找出问题的原因,是非常SB的
即使找了100种方法应付,也是SB的。
发在网上,鼓吹应付的这种方式 ,更SB
2010-05-27 17:19 | OO

# re: Hibernate之deleted object would be re-saved by cascade异常  回复  更多评论   

@- -
你太强悍了!我的问题被你一顶就破了!
2010-06-08 21:25 | chivas

# re: Hibernate之deleted object would be re-saved by cascade异常  回复  更多评论   

It is a good article.
2011-12-07 18:25 | mens moncler coats

# re: Hibernate之deleted object would be re-saved by cascade异常  回复  更多评论   

It is a good article.
2011-12-07 18:27 | mens moncler coats

# re: Hibernate之deleted object would be re-saved by cascade异常  回复  更多评论   

XD 谢谢Po主!
总之在级联属性里鼓捣,最后解决了问题。感谢!
2015-06-03 11:32 | 一番

只有注册用户登录后才能发表评论。


网站导航: