cuiyi's blog(崔毅 crazycy)

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

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

          在Hibernate中,刪除存在關(guān)聯(lián)關(guān)系的一個(gè)對(duì)象時(shí),會(huì)出現(xiàn) org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)這個(gè)異常

          如下:
          持久化類:
          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)品類別對(duì)象
           * 
          @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>

          測(cè)試代碼:
          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

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

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


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

          評(píng)論

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

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

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

          嗯;謝謝;

          及時(shí)截短了:)
          原先標(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ù)  更多評(píng)論   

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

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

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

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

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

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

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

          長(zhǎng)你媽個(gè)逼

          怎么決絕的問題 ? !

          2007-07-24 12:20 | 萬里

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

          http://www.aygfsteel.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)示的選項(xiàng)了;我用uuid.hex時(shí)是沒有問題的;但是用了native,就不行了,怎么辦?刪除啊!

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

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

          LZ 你太有才了 ! 謝了 !

          問題解決完畢 .

          推薦給大家一個(gè)新的架構(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ù)  更多評(píng)論   

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

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

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

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

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

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

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

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

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

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

          找方法應(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ù)  更多評(píng)論   

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

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

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

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

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

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

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

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 赤水市| 汉阴县| 大冶市| 武义县| 盐亭县| 寿宁县| 龙陵县| 宁晋县| 百色市| 大关县| 环江| 寿宁县| 乐陵市| 呼和浩特市| 句容市| 诸城市| 桂阳县| 苍梧县| 应用必备| 沧州市| 革吉县| 汕头市| 宁国市| 镶黄旗| 盖州市| 香格里拉县| 黄石市| 张家川| 邻水| 安图县| 嵩明县| 大方县| 龙海市| 五指山市| 肇州县| 开封县| 福海县| 宁安市| 淮安市| 金塔县| 连山|