I have a TestEntity that has an ArrayList of ChildEntities. I want to
get the Entity from the datastore, update an int field in the child
entity, and then swap the position of the two child entities.
However, as soon as I swap the entities, it appears as though the
updates to the int field are erased. Am I not allowed to store a JDO
persisted object in a temp variable to do the swap? Here is my test
code, followed by the Definitions of the Entities themselves. When I
step through the code in the debugger, my ChildEntity gets overwritten
or reset or whatever as soon as I copy the 2nd value into the 1st
position in the list.
thanks for any insight you can give as to how i can accomplish this
swap.
-peter
@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)); // at this point, 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;
}
}
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=.