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

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

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

    cuiyi's blog(崔毅 crazycy)

    記錄點滴 鑒往事之得失 以資于發(fā)展
    數(shù)據(jù)加載中……

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

    在Hibernate中,刪除存在關(guān)聯(lián)關(guān)系的一個對象時,會出現(xiàn) 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;

    /**
     * 產(chǎn)品類別對象
     * 
    @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;
        }
        
        
    /**
         * 由主來調(diào):是主添加
         * 
    @param productCategory
         
    */
        
    public void addCategory(ProductCategory productCategory) {
            productCategory.setParentCategory(
    this);
            childrenCategories.add(productCategory);
        }
        
        
    /**
         * 由主來調(diào);是從主刪除
         * 
    @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 解決關(guān)聯(lián)關(guān)系后,再刪除 :
    category2.getChildrenCategories().remove(category5);
            category5.setParentCategory(
    null);
            dao.removeProductCategory(category5.getId());
    方法3 在many-to-one方增加cascade 但值不能是none

    如果以上三個方案都失敗(哼哼~ 我用了5個小時才找出來的)
    檢查一下hashCode equals是否使用了id作為唯一標(biāo)示的選項了;我用uuid.hex時是沒有問題的;
    但是用了native,就不行了,怎么辦?刪除啊!

    也就是問題出現(xiàn)在本文給出的持久化類的hashCode equals方法身上


    posted on 2006-06-24 22:07 crazycy 閱讀(33705) 評論(18)  編輯  收藏

    評論

    # re: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)異常的解決  回復(fù)  更多評論   

    標(biāo)題太長了。
    2006-06-24 22:44 | dudu

    # re: Hibernate之deleted object would be re-saved by cascade 異常的解決  回復(fù)  更多評論   

    嗯;謝謝;

    及時截短了:)
    原先標(biāo)題:
    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 異常的解決  回復(fù)  更多評論   

    曾遇到與樓主同樣問題,僅想到方法一方法二而已,佩服~
    2006-07-06 08:45 | Y04069

    # re: Hibernate之deleted object would be re-saved by cascade 異常的解決  回復(fù)  更多評論   

    謝謝樓主,生Q^_^
    偶正為這個問題頭痛啊
    2006-09-08 08:41 | coolbechy

    # re: Hibernate之deleted object would be re-saved by cascade 異常的解決  回復(fù)  更多評論   

    標(biāo)題太長了。
    2007-05-09 22:56 | 監(jiān)聽器

    # re: Hibernate之deleted object would be re-saved by cascade 異常的解決  回復(fù)  更多評論   

    經(jīng)雞巴回復(fù)沒逼用的話 !

    長你媽個逼

    怎么決絕的問題 ? !

    2007-07-24 12:20 | 萬里

    # re: Hibernate之deleted object would be re-saved by cascade 異常的解決  回復(fù)  更多評論   

    http://www.tkk7.com/crazycy/archive/2006/07/07/57214.html


    方法1 刪除Set方的cascade.

    方法2 解決關(guān)聯(lián)關(guān)系后,再刪除.

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

    最后一招:
    檢查一下hashCode equals是否使用了id作為唯一標(biāo)示的選項了;我用uuid.hex時是沒有問題的;但是用了native,就不行了,怎么辦?刪除啊!

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

    # re: Hibernate之deleted object would be re-saved by cascade 異常的解決  回復(fù)  更多評論   

    LZ 你太有才了 ! 謝了 !

    問題解決完畢 .

    推薦給大家一個新的架構(gòu):

    * 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異常  回復(fù)  更多評論   

    不錯,學(xué)習(xí)了.
    2007-10-26 20:44 | 中華信鴿

    # re: Hibernate之deleted object would be re-saved by cascade異常  回復(fù)  更多評論   

    試試先,應(yīng)該可以,thank you.
    2007-11-14 18:39 | suhaoyuan

    # re: Hibernate之deleted object would be re-saved by cascade異常  回復(fù)  更多評論   

    看上去似乎能解決我的問題 :)
    2008-05-22 21:13 | camelwoo

    # re: Hibernate之deleted object would be re-saved by cascade異常  回復(fù)  更多評論   

    謝謝樓主
    問題解決啦
    2009-01-07 10:23 | 大是大非

    # re: Hibernate之deleted object would be re-saved by cascade異常  回復(fù)  更多評論   

    告訴你們一個簡單的辦法,你出現(xiàn)這個問題應(yīng)該是你查詢出來的語句的狀態(tài)還在[associations] 你只需要用 this.getHibernateTemplate().clear();他就會把這條語句的狀態(tài)改變,你就可以刪除了。
    2009-10-17 10:00 | - -

    # re: Hibernate之deleted object would be re-saved by cascade異常[未登錄]  回復(fù)  更多評論   

    找方法應(yīng)付,而沒有找出問題的原因,是非常SB的
    即使找了100種方法應(yīng)付,也是SB的。
    發(fā)在網(wǎng)上,鼓吹應(yīng)付的這種方式 ,更SB
    2010-05-27 17:19 | OO

    # re: Hibernate之deleted object would be re-saved by cascade異常  回復(fù)  更多評論   

    @- -
    你太強悍了!我的問題被你一頂就破了!
    2010-06-08 21:25 | chivas

    # re: Hibernate之deleted object would be re-saved by cascade異常  回復(fù)  更多評論   

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

    # re: Hibernate之deleted object would be re-saved by cascade異常  回復(fù)  更多評論   

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

    # re: Hibernate之deleted object would be re-saved by cascade異常  回復(fù)  更多評論   

    XD 謝謝Po主!
    總之在級聯(lián)屬性里鼓搗,最后解決了問題。感謝!
    2015-06-03 11:32 | 一番

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲人片在线观看天堂无码 | 日本视频在线观看永久免费| 精品熟女少妇aⅴ免费久久| 在线观看片免费人成视频播放| 国产日韩AV免费无码一区二区| 久久国产精品萌白酱免费| 国产成人免费午夜在线观看| 成人免费AA片在线观看| 国产在线观看www鲁啊鲁免费| 久久精品国产精品亚洲下载| 亚洲嫩草影院久久精品| 亚洲一卡2卡3卡4卡5卡6卡| 四虎精品成人免费视频| 日本一道本不卡免费| 无码精品A∨在线观看免费| 国产成人精品高清免费| 国产亚洲精品a在线观看app| 亚洲国产理论片在线播放| 久久亚洲中文无码咪咪爱| 国产97视频人人做人人爱免费| 久久ww精品w免费人成| 午夜时刻免费入口| 国产亚洲人成网站在线观看| 亚洲精品人成电影网| 色吊丝免费观看网站| 99久热只有精品视频免费观看17| 在线免费一区二区| 国产亚洲欧洲Aⅴ综合一区| 色婷五月综激情亚洲综合| av网站免费线看| 美女裸身网站免费看免费网站| 国产人成免费视频| 亚洲国产精品lv| 亚洲欧美日韩综合久久久| 两性色午夜视频免费播放| 无码中文字幕av免费放| 相泽亚洲一区中文字幕| 亚洲av无码不卡久久| 成人妇女免费播放久久久| 成年女人免费v片| 国产亚洲人成无码网在线观看 |