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 閱讀(33701) 評論(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.aygfsteel.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 | 一番

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


          網站導航:
           
          主站蜘蛛池模板: 天柱县| 远安县| 南京市| 宕昌县| 乌鲁木齐县| 讷河市| 勐海县| 宣化县| 吉首市| 平安县| 灵璧县| 淅川县| 东乡族自治县| 鸡东县| 沂源县| 柳州市| 石阡县| 大关县| 泽库县| 夏河县| 富顺县| 洛隆县| 隆回县| 镇平县| 泗阳县| 中西区| 靖安县| 建德市| 汉寿县| 正定县| 会东县| 林口县| 茌平县| 阳泉市| 汝南县| 城固县| 芮城县| 大田县| 长顺县| 深泽县| 和林格尔县|