2 Replies Latest reply on Nov 19, 2008 5:09 AM by mravikrish

    delete object from jointable

    mravikrish

      Hello,
      I am Ramu. I am having a problem with deleting objects from join table (many-to-many) relationship.my code is as follows

      Vendor child = this.manager.getReference(Vendor.class, vendorid );

      Collection listParents = child.getBuyers();
      Iterator iteratorParents = listParents.iterator();
      while (iteratorParents.hasNext() == true)
      {
      Buyer parentForChild = iteratorParents.next();
      parentForChild.getVendors().remove(child);
      }

      this.manager.remove(child);

      but it is deleting the child object from parent table also. i want to delete only from join table not from original table . any help appreciated.

      Thank u
      Ramu

        • 1. Re: delete object from jointable
          wolfgangknauf

          Hi,

          you mean you want to remove the child from all buyers, but keep the child?

          Then your code should be (last two lines are changed):

          Vendor child = this.manager.getReference(Vendor.class, vendorid );
          
          Collection listParents = child.getBuyers();
          Iterator iteratorParents = listParents.iterator();
          while (iteratorParents.hasNext() == true)
          {
           Buyer parentForChild = iteratorParents.next();
           parentForChild.getVendors().remove(child);
          }
          
          //remove ALL buyers from child:
          listParents.removeAll();
          
          //Now SAVE the child:
          this.manager.persist(child);


          Hope this helps

          Wolfgang

          • 2. Re: delete object from jointable
            mravikrish

            Thank u Wolfgang