Hi all,

I found another strange (unexpected behavior for me) with my two simple Parent and Child classes and Entity Groups.
ASAIK in a transaction can be manipulated any object as long as they belong to the same entity group.
Now, I cannot understand why I'm getting a Fatal exception when I try to persist in a transaction an object for whom I set the encoded key as child of the same parent.
Snippet in the following.

Hope, Someone can help!

Thanks

Here's the snippet and my classes:

----------------- SNIPPET ------------------------------
    public void testTransactions()  {
        String parentId = "parent";
        String childId_1 = "child-1";
        String childId_2 = "child-2";

        PersistenceManager pm = PMF.get().getPersistenceManager();
        pm.currentTransaction().begin();
        Parent parent = new Parent();
        parent.setId(parentId);
        Child child = new Child();
        child.setId(childId_1);
        child.setValue(100);
        parent.setChild(child);
        pm.makePersistent(parent);
        pm.currentTransaction().commit();
        pm.close();

        pm = PMF.get().getPersistenceManager();
        pm.currentTransaction().begin();
        String parentEncodedKey = KeyFactory.createKeyString(Parent.class.getSimpleName(), parentId);
        parent = pm.getObjectById(Parent.class, parentEncodedKey);
        assertEquals(100, parent.getChild().getValue());
        parent.getChild().setValue(1000);
        child = new Child();
        child.setId(childId_2);
        Key child2Key = KeyFactory.createKey(KeyFactory.stringToKey(parentEncodedKey), Child.class.getSimpleName(), childId_2);
        child.setEncodedKey(KeyFactory.keyToString(child2Key));
        child.setValue(10);
        pm.makePersistent(child);
        pm.makePersistent(parent);
        pm.currentTransaction().commit();
        pm.close();

        pm = PMF.get().getPersistenceManager();
        pm.currentTransaction().begin();       
        parentEncodedKey = KeyFactory.createKeyString(Parent.class.getSimpleName(), parentId);
        parent = pm.getObjectById(Parent.class, parentEncodedKey);
        assertEquals(1000, parent.getChild().getValue());
        pm.currentTransaction().commit();
        pm.close();

    }       
-----------------------------------------------
---------------- PARENT -------------------------------
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Parent implements Serializable {

    private static final long serialVersionUID = -3051163582728562957L;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    protected String encodedKey;

    @Persistent
    @Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
    private String id;
   
    @Persistent(defaultFetchGroup="true")
    @Element(dependent="true")
    private Child child;
   
    public String getEncodedKey() {
        return encodedKey;
    }

    public void setEncodedKey(String encodedKey) {
        this.encodedKey = encodedKey;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Child getChild() {
        return child;
    }

    public void setChild(Child child) {
        this.child = child;
    }
   
}
-----------------------------------------------
------------------- CHILD ----------------------------
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Child implements Serializable {

    private static final long serialVersionUID = -3051163582728562957L;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    protected String encodedKey;

    @Persistent
    @Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
    private String id;
   
    @Persistent
    private long value;
   
    public String getEncodedKey() {
        return encodedKey;
    }

    public void setEncodedKey(String encodedKey) {
        this.encodedKey = encodedKey;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public long getValue() {
        return value;
    }

    public void setValue(long value) {
        this.value = value;
    }
   
}
-----------------------------------------------



--~--~---------~--~----~------------~-------~--~----~
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=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to