makePersistent 正在将元素附加到我的列表中,而不是替换列表

发布于 2024-09-29 00:09:25 字数 1310 浏览 1 评论 0原文

我使用 GAE 数据存储和 JDO 来保存课程信息。每个课程都有一个 GradeDefinition 对象列表(例如“A=90%+”、“B=80%+”等)

当我尝试更改列表时,新元素只是附加在旧元素后面。我怀疑我使用可分离对象和持久性管理器的方式有问题。

基本概要:

@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.
}

然后,当我想要更新课程中的 GradeDefinition 对象列表时,我:

  1. 获取课程对象(它自动获取其 GradingPolicy,它自动获取其 GradeDefinition 对象列表),
  2. 准备我的新列表等级定义与

    列表newDefinitions = new ArrayList(); newDefinitions.add(new GradeDefinition("X")); //例如 newDefinitions.add(new GradeDefinition("Y"));

  3. 通过调用重置列表 course.getGradingPolicy.setGradeDefinitions(newDefinitions);

  4. 通过调用 persistenceManager.makePersistent(course); 来保留更改;

但是,如果评分政策最初有 A、B 和 C它现在包含 A、B、C、X 和 Y!

我需要做些什么来清除列表吗?我需要手动删除旧的 GradeDefinitions 吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-10-06 00:09:25

注意多个 PersistanceManager 之间的不一致,或者更好的是,一次只有一个 PersistanceManager 浮动。

Watch out for inconsistencies between multiple PersistanceManagers, or even better, only have a single PersistanceManager floating around at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文