-->
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.  [ 1 post ] 
Author Message
 Post subject: Many-to-one bidirectionnal relation to itself ?
PostPosted: Wed Oct 12, 2005 5:47 am 
Newbie

Joined: Thu Feb 03, 2005 11:36 am
Posts: 11
Hello, I have a class with a bidirectionnal relation to itself : a category can be have several child and have one parent.
Everything is fine except when I try to change the parentcategory. The category is "moved" to the new one but is not deleted from on the old one.
So I get two duplicated categories...

What's wrong ?

Thank you

Hibernate version: 3.0.5

Mapping documents:
Code:
<class name="Category" table="category">
      <id name="id" type="long" column="id">
         <generator class="native"/>
      </id>
      
      <!-- Properties -->
      <property name="title" column="title" type="string" length="64" not-null="true"/>
      
      <!-- owns Several child categories -->
      <set name="childCategories" cascade="all-delete-orphan" inverse="true" lazy="true">
         <key column="parent_category_id"/>
         <one-to-many class="Category"/>
      </set>
      <!-- And optionnaly a parent (can be a root also) -->
      <many-to-one name="parentCategory" cascade="none" outer-join="false"
         class="Category">
         <column name="parent_category_id" not-null="false"/>
      </many-to-one>
      
      <!-- Contains messages -->
      <set name="messages" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key column="categoryid"/>
         <one-to-many class="Message"/>
      </set>
   </class>

Code between sessionFactory.openSession() and session.close():
Code:
        Category parentCategory = category.getParentCategory();
        if(parentCategory != null)
            parentCategory = categoryDAO.getCategory(parentCategory.getId()); // reload parent

        if(category.getId() == null) // New category
/* skip */
        else
        {
            saveCategory = categoryDAO.getCategory(category.getId()); // get cat from DB
            // Is there a parent ? Has the parent changed ?
            if(parentCategory != null && category.getParentCategory().getId() != saveCategory.getParentCategory().getId())
            {
                saveCategory.getParentCategory().getChildCategories().remove(saveCategory);
                parentCategory.getChildCategories().add(saveCategory);
            }
            shallowCopy(category,saveCategory); // Just simple properties are copied here
        }
        categoryDAO.makePersistent(saveCategory);


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

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.