-->
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: problem with deleting item from set
PostPosted: Mon Mar 26, 2007 3:54 am 
Newbie

Joined: Mon Mar 26, 2007 2:59 am
Posts: 1
Hello.

i have two classes with many-to-many relationship, with bidirectional link.

the problem is, that in one class i cannot remove elements from a set that makes one part of that link, using remove()

Code:
stuff[i].getCategories().remove(currentCategory); //works
currentCategory.getStuff().remove(stuff[i]); //does not remove element from collection; returns false

both objects are in transient state, but if i attach them before doing this, it makes no difference.
however, if i try to remove object from collection using iterator, it works
Code:
stuff[j].getCategories().remove(currentCategory);
long id = stuff[i].getId();
for(Iterator<Stuff> it = currentCategory.getStuff().iterator(); it.hasNext();)
{
    Stuff s = it.next();
    if(s.getId() == stuff[i].getId())
    {
        it.remove();       
        break;                                       
    }
}       

here is an excerpt from declaration of both classes
Code:
...
public class Stuff implements Comparable, Serializable{       
...
private java.util.SortedSet<Category> categories;
   
   /**
    * @hibernate.set
    *     role="categories"
    *     table="CATEGORY_TO_STUFF"
    *      sort="natural"
    *     batch-size="50"
    *     cascade="all"
    *     inverse="true"
    *     lazy="true"
    * @hibernate.collection-key
    *     column="CATEGORY_FK"
    * @hibernate.collection-many-to-many
    *     class="imarine2.Category"
    *     column="STUFF_FK"
    */
   public java.util.SortedSet<Category> getCategories()
   {
       return this.categories;
   }
   
   protected void setCategories(java.util.SortedSet<Category> categories)
   {
       this.categories = categories;
   }
... 
   public Stuff()
   {
       categories = new TreeSet<Category>();       
       ...
   }
   
    public int compareTo(Object object) {
        Stuff stuff = (Stuff)object;       
        if(stuff.id < id)
            return -1;       
        if(stuff.id > id)
            return 1;       
        return 0;
    }
   
    public boolean equals(Object o)
    {       
        if(!(o instanceof Stuff))
            return false;           
        if(((Stuff)o).getId() == getId())
            return true;       
        return false;
    }
}

Code:
...
public class Category implements Comparable, Serializable{   
...
   private java.util.SortedSet<Stuff> stuff;
   
   /**
    * @hibernate.set
    *     role="stuff"
    *     table="CATEGORY_TO_STUFF"
    *     sort="natural"
    *     batch-size="50"
    *     cascade="all"
    *     inverse="false"
    *     lazy="true"
    * @hibernate.collection-key
    *     column="STUFF_FK"
    * @hibernate.collection-many-to-many
    *     class="imarine2.Stuff"
    *     column="CATEGORY_FK"
    *
    */
   public java.util.SortedSet<Stuff> getStuff() {
       return this.stuff;
   }
   
   protected void setStuff(java.util.SortedSet<Stuff> stuff) {
       this.stuff = stuff;
   }
...
   public Category()
   {     
       stuff = new TreeSet<Stuff>();     
       ...
   }
   
   public boolean equals(Object o)
   {
       if(!(o instanceof Category))
            return false;       
       if(((Category)o).getId() == this.getId())
           return true;       
       return false;
   }

    public int compareTo(Object object) {
        Category category = (Category)object;
       
        if(category.id < id)
            return -1;       
        if(category.id > id)
            return 1;       
        return 0;               
    }
   
    }


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.