I have a TestEntity that has an ArrayList of ChildEntities. They are joined in a owned relationship. 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 position 0 in the list. I'm afraid there's something about how JDO works that I am missing.
@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;
}