views:

32

answers:

0

Hello

I want to change order of objects in a collection, and then to access+modify fields of those items. Unfortunately the items somehow become 'deleted'.

This is what I do...

if(someCondition)
{
  MainEvent mainEvent = pm.getObjectById(MainEvent.class, mainEventKey);
  /*
   * events in the original order 
   * MainEvent.subEvents field is not in default fetch group, 
   * therefore I also tried to add the named group into the
   * persistenceManeger fetch plan, no difference
   * (mainEvent is not instance of the Event sub/class BTW)
   */
  List<Event> subEvents = mainEvent.getSubEvents();

  // re-arrange the events according to keysOrdered {
  Map<Key, Event> eventMap = new HashMap<Key, Event>();
  for(Event event : subEvents)
    eventMap.put(event.getKey(), event);

  List<Event> eventsOrdered = new LinkedList<Event>();
  for(Key eventKey : keysOrdered)
    eventsOrdered.add(eventMap.put(eventKey, eventMap.get(eventKey)));
  // }

  // put the re-arranged items back into the collection property {
  subEvents.clear();
  subEvents.addAll(eventsOrdered);
  // }

  pm.makePersistent(mainEvent);
  eventsOrdered = subEvents;
}
else
  eventsOrdered = getEventsUsingAlternateApproach();

/*
 * so by now the mainEvent variable does not exist; 
 * could it be this lead the persistence manager to mark 
 * my events as abandoned/obsolete/invalid/deleted...?
 */

for(Event event : eventsOrdered)
  event.setDate(new Date()); // -> "Cannot write fields to a deleted object"

What am I doing wrong please?