<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(33688) 評論(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://m.tkk7.com/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 | 一番

    只有注冊用戶登錄后才能發表評論。


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 亚洲一区二区三区无码国产| 亚洲国产人成在线观看69网站| 亚洲毛片免费观看| 国产亚洲人成无码网在线观看 | 亚洲成AV人在线观看网址| 亚洲精品宾馆在线精品酒店| 国产精品爱啪在线线免费观看| 亚洲欧洲久久精品| 久久精品女人天堂AV免费观看| 最新亚洲精品国偷自产在线| 成人毛片免费视频| 亚洲av成人片在线观看| 亚洲成a人无码av波多野按摩| 成人免费网站视频www| 亚洲熟妇av一区二区三区漫画| 91精品全国免费观看青青| 亚洲国产香蕉碰碰人人| 五月婷婷在线免费观看| 亚洲日韩精品无码一区二区三区| 久久免费观看视频| 亚洲精品美女在线观看| 好吊妞视频免费视频| 日本高清不卡中文字幕免费| 好男人视频社区精品免费| 国产精品亚洲二区在线| 在线亚洲人成电影网站色www| 免费人成在线观看网站| 中文字幕亚洲日韩无线码| 女人体1963午夜免费视频| 亚洲三级在线免费观看| 全部免费a级毛片| 国产免费一区二区视频| 亚洲一区二区三区丝袜| 国产成人A亚洲精V品无码| 1000部拍拍拍18免费网站| 国产综合激情在线亚洲第一页| 久久精品国产亚洲网站| 成年性生交大片免费看| 天黑黑影院在线观看视频高清免费 | 在线jlzzjlzz免费播放| 国产精品无码永久免费888|