makePersistent 正在将元素附加到我的列表中,而不是替换列表
我使用 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 对象列表时,我:
- 获取课程对象(它自动获取其 GradingPolicy,它自动获取其 GradeDefinition 对象列表),
准备我的新列表等级定义与
列表
newDefinitions = new ArrayList (); newDefinitions.add(new GradeDefinition("X")); //例如 newDefinitions.add(new GradeDefinition("Y")); 通过调用重置列表
course.getGradingPolicy.setGradeDefinitions(newDefinitions);
- 通过调用
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:
- Get the course object (which automatically fetches its GradingPolicy, which automatically fetches its list of GradeDefinition objects),
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"));Reset the list by calling
course.getGradingPolicy.setGradeDefinitions(newDefinitions);
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注意多个 PersistanceManager 之间的不一致,或者更好的是,一次只有一个 PersistanceManager 浮动。
Watch out for inconsistencies between multiple PersistanceManagers, or even better, only have a single PersistanceManager floating around at a time.