使用Google Appengine上的JDO在列表中交换两个元素

时间:2009-11-17 20:13:29

标签: java google-app-engine jdo datanucleus

我有一个具有ChildEntities的ArrayList的TestEntity。他们加入了自己的关系。我想要 从数据存储中获取实体,更新子节点中的int字段 实体,然后交换两个子实体的位置。 但是,只要我交换实体,它就好像是 对int字段的更新将被删除。我不允许存储JDO 在temp变量中持久化对象来进行交换?这是我的测试 代码,然后是实体本身的定义。当我 单步执行调试器中的代码,我的ChildEntity会被覆盖 我将第二个值复制到列表中的位置0时立即重置或复位。我担心JDO的工作原理是我缺少的。

@Test
public void testSwap() {
    PersistenceManager pm = PMF.get().getPersistenceManager();

    TestEntity te = new TestEntity();
    pm.makePersistent(te);
    te.getChildEntities().add(new TestChildEntity("a"));
    te.getChildEntities().add(new TestChildEntity("b"));
    long id = te.getId();
    pm.close();
    pm = PMF.get().getPersistenceManager();
    te = pm.getObjectById(TestEntity.class, id);

    List<TestChildEntity> children = te.getChildEntities();
    for (TestChildEntity tce : children) {
        tce.setFoo(3);
    }

    TestChildEntity temp = children.get(0);
    children.set(0, children.get(1));   

        // after the above line executes, the object referred to by temp is overwritten/reset when i watch in debugger.

    children.set(1, temp);
    assertEquals(3, children.get(0).getFoo());  // these asserts will both fail.
    assertEquals(3, children.get(1).getFoo());

}

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class TestEntity {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Long id;

        @Persistent(mappedBy = "parent")
        private List<TestChildEntity> childEntities;

        public List<TestChildEntity> getChildEntities() {
                return childEntities;
        }

        public Long getId() {
                return id;
        }

        public static void demonstrateProblemWithRemove() {
                PersistenceManager pm = PMF.get().getPersistenceManager();
                TestEntity te = new TestEntity();
                pm.makePersistent(te);
                te.getChildEntities().add(new TestChildEntity("a"));
                te.getChildEntities().remove(0);
                pm.close();
        }

}

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class TestChildEntity {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value
= "true")
        private String encodedKey;

        @Persistent
        @Extension(vendorName = "datanucleus", key = "gae.pk-name", value =
"true")
        private String keyName;

        @Persistent
        private int foo;

        @Persistent
        private TestEntity parent;

        public TestChildEntity(String k) {
                this.keyName = k;
                this.foo = 1;
        }

        public int getFoo() {
                return foo;
        }

        public void setFoo(int foo) {
                this.foo = foo;
        } 

1 个答案:

答案 0 :(得分:0)

尝试将defaultFetchGroup =“true”添加到TestEntity.childEntities上方的@Persistent注释

默认情况下,不会从数据存储中获取childEntities(据我所知,它是JDO行为,没有特定于AppEngine)。在你的情况下,我想,他们甚至没有写,因为你在添加孩子之前调用makePersistent。