Tobias Wolff | 31 Jul 2012 23:00
Favicon

Deleting n:m relationships

Hi,

I am currently trying to do the following in Cayenne 3.0.2:

  * fetch an object A
  * delete some related objects which are related to A in an n:m relation
  * add new objects to A (same n:m relation)

I have undergone a couple of java.util.ConcurrentModificationException 
and org.apache.cayenne.validation.ValidationException exception without 
being able to fix them. Here is some code, but a general hint on how to 
achieve that (especially when to do commits) would be much appreciated.

  * Class Forecast
  * Class LineItem
  * Class ForecastCondendingLineItem (n:m relation between Forecast and
    LineItem)

I have an object F of class Forecast which I have pulled from the 
database. Now I try to delete the n:m relations like this:

for(ForecastContendingLineItem temp : 
F.getForecastContendingLineItemArray()) {
   F.removeFromForecastContendingLineItemArray(temp);
   context.deleteObject(temp);
}

This results in the ConcurrentModificationException. I also tried this:

context.deleteObjects(F.getForecastContendingLineItemArray());
(Continue reading)

Tobias Wolff | 1 Aug 2012 13:08
Favicon

Re: Deleting n:m relationships

Hi again,

so, I now tried this:

         // Delete any condending ads
         for(ForecastContendingLineItem temp : 
_forecastItem.getForecastContendingLineItemArray()) {
_forecastItem.removeFromForecastContendingLineItemArray(temp);
         }

         // Now deal with the contending ads
         LineItem contendingLineItem = null;
         ForecastContendingLineItem relation = null;
         for(Integer tempId : result.getContendingAdIds()) {
           contendingLineItem = 
LineItemDfpInfo.getLineItemByDfpId(config.context(), tempId);
           if(contendingLineItem != null) {
             relation = new ForecastContendingLineItem();
             relation.setToForecast(_forecastItem);
             relation.setToLineItem(contendingLineItem);
             relation.setUsedImpressions(0L);
             relation.setOsi(0L);

_forecastItem.addToForecastContendingLineItemArray(relation);
           }
         }

So first, I am trying to remove the existing items from the list (first 
3 lines). Then I am adding the new once. I now get this:

(Continue reading)

Emanuele Maiarelli | 1 Aug 2012 15:16

Re: Deleting n:m relationships


you should create 

ForecastContendingLineItem from a contex using
context.newObject(ForecastContendingLineItem.class);

On Wed, 1 Aug 2012
13:08:36 +0200, Tobias Wolff wrote: 

> Hi again,
> 
> so, I now tried
this:
> 
> // Delete any condending ads
> for(ForecastContendingLineItem
temp : 
> _forecastItem.getForecastContendingLineItemArray()) {
>
_forecastItem.removeFromForecastContendingLineItemArray(temp);
> }
> 
>
// Now deal with the contending ads
> LineItem contendingLineItem =
null;
> ForecastContendingLineItem relation = null;
> for(Integer tempId
: result.getContendingAdIds()) {
> contendingLineItem = 
(Continue reading)

Emanuele Maiarelli | 1 Aug 2012 16:12

Re: Deleting n:m relationships


Also try to replace 

 // Delete any condending ads

for(ForecastContendingLineItem temp :

_forecastItem.getForecastContendingLineItemArray())
{
_forecastItem.removeFromForecastContendingLineItemArray(temp);

}

with

List toDelete=new
ArrayList();

toDelete.addAll(_forecastItem.getForecastContendingLineItemArray());

for(ForecastContendingLineItem temp : toDelete) {

_forecastItem.removeFromForecastContendingLineItemArray(temp);

 }

context.DeleteObjects(toDelete);

This will avoid either
ConcurrentModificationException, and also the fact that with just
(Continue reading)

Emanuele Maiarelli | 1 Aug 2012 16:23

Re: Deleting n:m relationships

forgot to disable html formatting of email client, and it have been 
formatted badly :)

List<ForecastContendingLineItem> toDelete=new 
ArrayList<ForecastContendingLineItem>();

toDelete.addAll(_forecastItem.getForecastContendingLineItemArray());

       for(ForecastContendingLineItem temp : toDelete) {
        _forecastItem.removeFromForecastContendingLineItemArray(temp);

      }
  context.deleteObjects(toDelete);

Regards,

--

-- 

Tobias Wolff | 1 Aug 2012 16:34
Favicon

Re: Deleting n:m relationships

Thanks Emanuele,

I changed it now to:

         // Delete any condending ads
         List<ForecastContendingLineItem> toDelete = new 
ArrayList<ForecastContendingLineItem>();
         for(ForecastContendingLineItem temp : 
_forecastItem.getForecastContendingLineItemArray()) {
           toDelete.add(temp);
         }
         config.context().deleteObjects(toDelete);

         // Now deal with the contending ads
         LineItem contendingLineItem = null;
         ForecastContendingLineItem relation = null;
         for(Integer tempId : result.getContendingAdIds()) {
           contendingLineItem = 
LineItemDfpInfo.getLineItemByDfpId(config.context(), tempId);
           if(contendingLineItem != null) {
             relation = 
config.context().newObject(ForecastContendingLineItem.class);
             relation.setToForecast(_forecastItem);
             relation.setToLineItem(contendingLineItem);
             relation.setUsedImpressions(0L);
             relation.setOsi(0L);

_forecastItem.addToForecastContendingLineItemArray(relation);
           }
         }
(Continue reading)


Gmane