-->
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.  [ 2 posts ] 
Author Message
 Post subject: Deleting collection element doesn't work
PostPosted: Fri Dec 29, 2006 10:59 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Hi

I've got an Offer (parent object) which contains a collection of Conditions. I'm using a bag, defined as follows:

Code:
        <bag name="conditions" table="MORTGAGE_OFFER_CONDITION" lazy="true">
            <key column="OFFER_ID"/>
            <many-to-many class="com.lsb.uk.mqs.value.ConditionValue"
                          column="CONDITION_ID"/>
        </bag>


When I delete an element from a JUnit test, which calls the DAO method directly, it works fine.

But, when called by the system this lives in (via a BO layer, which is where the transaction level is set) a call to .remove() on the collection, makes no difference, even if I flush the session.

This is the method code:

Code:
   public void deleteMortgageOfferCondition(int offerId, int conditionId) {
      OfferValue offer = (OfferValue)getHibernateTemplate().load(OfferValue.class, new Integer(offerId));
      ConditionValue cond = (ConditionValue)getHibernateTemplate().load(ConditionValue.class, new Integer(conditionId));
      offer.getConditions().remove(cond);      
      getHibernateTemplate().update(offer);
   };


Anyone know why it's not working? PS the update() call at the end doesn't make any difference either.

Thanks

Richard


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 29, 2006 11:12 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
I'll answer my own question - realised what I was doing wrong about 1 minute after this post....

I was looking up the child record, and trying to delete it from the collection. But, the child record isn't the type of object in the collection - its a many-to-many, so my call to remove() didn't find it in the collection, and so nothing was deleted.

This is the method now, and it works

Code:
   public void deleteMortgageOfferCondition(int offerId, int conditionId) {
      OfferValue offer = (OfferValue)getHibernateTemplate().load(OfferValue.class, new Integer(offerId));
      //ConditionValue cond = (ConditionValue)getHibernateTemplate().load(ConditionValue.class, new Integer(conditionId));
      for(int i=0; i<offer.getConditions().size(); i++) {
         ConditionValue v = (ConditionValue)offer.getConditions().get(i);
         if(v.getConditionId() == conditionId) {
            offer.getConditions().remove(i);
         }
      }
      getHibernateTemplate().update(offer);
   };


Hope this helps someone out in future


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.