-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: update() Does not delete cascade.
PostPosted: Wed Oct 12, 2005 9:49 am 
Newbie

Joined: Wed Oct 12, 2005 9:43 am
Posts: 8
I have a set of objects. This set a less than existing in database. I call update for the head of set. It does not delete cascade sub Elements. But at all I am set cascade="all". Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 10:21 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
That made my head hurt but maybe try "all-delete-orphan"?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 12:00 pm 
Newbie

Joined: Wed Oct 12, 2005 9:43 am
Posts: 8
Thanks. But now I have new problem . I have in set really only one object , but to this object many references from others. It says me that - a different object with the same identifier value was already associated with the session: 102


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 12:01 am 
Newbie

Joined: Fri Apr 01, 2005 5:21 am
Posts: 11
Can you paste the code between open session and close session?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 3:26 am 
Newbie

Joined: Wed Oct 12, 2005 9:43 am
Posts: 8
Code:
<class name="Website" table="WEBSITE">
      <id name="id" column="ID" type="java.lang.Long">
         <generator class="sequence">
.......
         </generator>
      </id>

         not-null="true" />
      <property name="name" column="NAME" type="java.lang.String"
         not-null="true" />


      <set name="sitemapElementSet" cascade="all-delete-orphan"
         inverse="true"
         where="PARENT_ID is NULL">
         <key column="WEBSITE_ID" />
         <one-to-many class="SitemapElement" />
      </set>

      <set name="menuSet" cascade="all-delete-orphan" inverse="true">
         <key column="WEBSITE_ID" />
         <one-to-many class="Menu" />
      </set>
</class>


Code:
<class name="SitemapElement" table="SITEMAP_ELEMENT">
      <id name="id" column="ID" type="long">
         <generator class="sequence">
........
         </generator>
      </id>

      <discriminator column="SITEMAP_ELEMENT_TYPE" type="string" />

      <many-to-one name="website" column="WEBSITE_ID" class="Website"
         not-null="true" />

      <property name="name" column="NAME" type="string"
         not-null="true" />

      <subclass name="SitemapFolder"
         discriminator-value="sm_folder">
         <set name="subElementSet" cascade="all"   >
            <key column="PARENT_ID" />
            <one-to-many class="SitemapElement" />
         </set>
      </subclass>

      <subclass name="SitemapLink" discriminator-value="sm_link">

         <set name="menuLinkSet" inverse="true" cascade="save-update">
            <key column="LINK_ID" />
            <one-to-many class="MenuLink" />
         </set>

         <property name="url" column="URL" type="java.lang.String"
            not-null="true" />

      </subclass>

   </class>


Code:
<class name="Menu" table="MENU">
      <id name="id" column="ID" type="long">
         <generator class="sequence">
         ......
         </generator>
      </id>

      <property name="name" column="NAME" type="string"
         not-null="true" />

      <many-to-one name="website" column="WEBSITE_ID" class="Website"
         not-null="true" />

      <set name="menuElementSet" inverse="true"
         cascade="save-update" table="MENU_ELEMENT"
                  where="PARENT_ID is NULL">
         <key column="MENU_ID" />
         <one-to-many class="MenuElement" />
      </set>
   </class>

Code:
   <class name="MenuElement" table="MENU_ELEMENT">
      <id name="id" column="ID" type="long">
         <generator class="sequence">
.......
         </generator>
      </id>

      <discriminator column="MENU_ELEMENT_TYPE" type="string" />

      <property name="name" column="NAME" type="string"
         not-null="true" />

      <many-to-one name="menu" column="MENU_ID" class="Menu"
         not-null="true" cascade="save-update"/>

      <subclass name="MenuFolder" discriminator-value="menufolder">
         <set name="subElementSet" cascade="all"   >
            <key column="PARENT_ID" />
            <one-to-many class="MenuElement" />
         </set>
      </subclass>

      <subclass name="MenuLink" discriminator-value="menulink">
         <many-to-one name="link" column="LINK_ID"
            class="SitemapLink" not-null="true" />
         <many-to-one name="layout" column="LAYOUT_ID" class="Layout"
            cascade="all" />
      </subclass>
   </class>


It is my data schema.
Image

I am using Spring HibernateTemplate . So my code easy
Code:
      getHibernateTemplate().saveOrUpdate(newWebsite);

I am don't include java classes because they according mapping. I am using this equals and hashcode methods
Code:
public int hashCode() {
      if (this.hashValue == 0) {
         int result = 17;
         int idValue = this.getId() == null ? 0 : this.getId().hashCode();
         result = result * 37 + idValue;
         this.hashValue = result;
      }
      return this.hashValue;
}

Code:
public boolean equals(Object rhs) {
      if (rhs == null)
         return false;
      if (!(rhs instanceof  Current_Object))
         return false;
      Current_object that = (Current_object) rhs;
      if (this.getId() == null || that.getId() == null)
         return false;
      return (this.getId().equals(that.getId()));
}


equals and hashcode I am overriding for all my classes.


Top
 Profile  
 
 Post subject: I have simplified now it looks like
PostPosted: Thu Oct 13, 2005 12:50 pm 
Newbie

Joined: Wed Oct 12, 2005 9:43 am
Posts: 8
Code:
<class name="Website" table="WEBSITE" dynamic-insert="true"
      dynamic-update="true">

      <set name="menuSet" cascade="all-delete-orphan" inverse="true">
         <key column="WEBSITE_ID" />
         <one-to-many class="Menu" />
      </set>

      <set name="sitemapElementSet" cascade="all-delete-orphan"
         inverse="true"where="PARENT_ID is NULL">
         <key column="WEBSITE_ID" />
         <one-to-many class="SitemapElement" />
      </set>
   </class>


Code:
<class name="Menu" table="MENU" dynamic-insert="true"
      dynamic-update="true" optimistic-lock="all" select-before-update="true">
      <many-to-one name="website" column="WEBSITE_ID" class="Website"
         not-null="true" />

      <set name="menuElementSet" cascade="all-delete-orphan"
         table="MENU_ELEMENT"    where="PARENT_ID is NULL" inverse="true">
         <key column="MENU_ID" />
         <one-to-many class="MenuElement" />
      </set>
   </class>

Code:
<class name="SitemapElement" table="SITEMAP_ELEMENT"
      dynamic-insert="true" dynamic-update="true" optimistic-lock="all" select-before-update="true">
      
      <discriminator column="SITEMAP_ELEMENT_TYPE" type="string" />

      <many-to-one name="parent" column="PARENT_ID"
         class="SitemapElement" />

      <many-to-one name="website" column="WEBSITE_ID" class="Website"
         not-null="true" />

      <subclass name="SitemapFolder"
         discriminator-value="sm_folder">
         <set name="subElementSet" cascade="all-delete-orphan" inverse="true">
            <key column="PARENT_ID" />
            <one-to-many class="SitemapElement" />
         </set>
      </subclass>

      <subclass name="SitemapLink" discriminator-value="sm_link" >

         <set name="areaSet" inverse="true" lazy="true">
            <key column="LINK_ID" />
            <one-to-many class="Area" />
         </set>

      <property name="url" column="URL" type="java.lang.String"
            not-null="true" />

      </subclass>


Code:
<class name="MenuElement" table="MENU_ELEMENT" dynamic-insert="true"
      dynamic-update="true" optimistic-lock="all" select-before-update="true">


      <discriminator column="MENU_ELEMENT_TYPE" type="string" />
      <many-to-one name="menu" column="MENU_ID" class="Menu"
         not-null="true" />
      <many-to-one name="parent" column="PARENT_ID"
         class="MenuElement" />

      <subclass name="MenuFolder" discriminator-value="menufolder">
         <set name="subElementSet" cascade="all-delete-orphan"
            sort="com.snapactive.utils.IndexComparator" inverse="true">
            <key column="PARENT_ID" />
            <one-to-many class="MenuElement" />
         </set>
      </subclass>

      <subclass name="MenuLink" discriminator-value="menulink">
         <many-to-one name="link" column="LINK_ID"
            class="SitemapLink" not-null="true" cascade="all-delete-orphan"/>
      </subclass>
   </class>

But it still throw NonUniqueObjectException


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 1:02 pm 
Newbie

Joined: Wed Oct 12, 2005 9:43 am
Posts: 8
What I want reach. When I store data I want for first save SiteMapElements and then menus , when delete some elements I want delete for first element from Menu and then from Sitemap. Or if I start delete from siteMap i want delete cascading it. Thank you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.