views:

14

answers:

0

I'm using the GAE datastore with JDO to hold course information. Each Course has a List of GradeDefinition objects (e.g. "A=90%+", "B=80%+", etc)

When I try to change the List, the new elements are simply appended behind the old elements. I suspect there is a problem with the way I am using detachable objects and persistence managers.

A basic rundown:

@PersistenceCapable(detachable = "true")
public class Course
{
    @Persistent(defaultFetchGroup = "true")
    private GradingPolicy gradingPolicy;
}

@PersistenceCapable(detachable = "true")
public class GradingPolicy
{
    @Persistent(defaultFetchGroup = "true")
    private List<GradeDefinition> gradeDefinitions;
}

@PersistenceCapable(detachable = "true")
public class GradeDefinition
{
    @Persistent
    private String gradeName; //e.g. "A", "B", etc.
}

Then, when I want to update the list of GradeDefinition objects in a Course, I:

  1. Get the course object (which automatically fetches its GradingPolicy, which automatically fetches its list of GradeDefinition objects),
  2. Prepare my new list of GradeDefinitions with

    List<GradeDefinition> newDefinitions = new ArrayList<GradeDefinitions>(); newDefinitions.add(new GradeDefinition("X")); //for example newDefinitions.add(new GradeDefinition("Y"));

  3. Reset the list by calling course.getGradingPolicy.setGradeDefinitions(newDefinitions);

  4. Persist the changes by calling persistenceManager.makePersistent(course);

But, if the grading policy originally had A, B, and C in it, it now holds A, B, C, X, and Y!

Is there something I need to do to clear the list? Do I need to manually delete the old GradeDefinitions?