有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个TestEntity,它有一个childentity的数组列表。他们以一种自有的关系结合在一起。我想 从数据存储中获取实体,更新子对象中的int字段 实体,然后交换两个子实体的位置。 但是,只要我交换了实体,就好像 对int字段的更新将被删除。我不允许存储JDO吗 在临时变量中保留对象以进行交换?这是我的测验 代码,然后是实体本身的定义。当我 通过调试程序中的代码,my 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) 个答案

  1. # 1 楼答案

    尝试将defaultFetchGroup=“true”添加到TestEntity上方的@Persistent注释中。儿童实体

    默认情况下,不会从数据存储中获取子实体(据我所知,这是JDO行为,与AppEngine无关)。在您的情况下,我想,它们甚至没有编写,因为您在添加子级之前调用makePersistent