This enhanced version works...
I encourage you to use Keys as Encoded Strings so u may use the same
EntityClasses also on the client side :)

According to:
http://code.google.com/intl/de-DE/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Keys

If the app creates a new object and gives it the same string ID as
another object of the same kind (and the same entity group parent),
saving the new object overwrites the other object in the datastore.

Due to the current bug u may also need to delete all previous old
children manually if your intention was to update the object tree ...

I tested this code ... it works =>

@PersistenceCapable
public class A {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value
= "true")
        public String key;
        @Persistent
        @Extension(vendorName = "datanucleus", key = "gae.pk-name", value =
"true")
        public String keyName;
        @Persistent(defaultFetchGroup = "true", mappedBy = "a")
        @Element(dependent = "true")
        public List<B> bList = new ArrayList<B>();
}

@PersistenceCapable
public class B {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value
= "true")
        public String key;
        @Persistent
        @Extension(vendorName = "datanucleus", key = "gae.pk-name", value =
"true")
        public String keyName;
        @Persistent(defaultFetchGroup = "true", mappedBy = "b")
        @Element(dependent = "true")
        public List<C> cList = new ArrayList<C>();
        @Persistent
        public A a;
}

@PersistenceCapable
public class C {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value
= "true")
        public String key;
        @Persistent
        @Extension(vendorName = "datanucleus", key = "gae.pk-name", value =
"true")
        public String keyName;
        @Persistent
        public B b;
}


PersistenceManager pm = PMF.get().getPersistenceManager();

A a = null;
B b = null;
C c = null;

// 1) If A does not exist persist A
try {
        a = pm.getObjectById(A.class, "A");// check if A exist
} catch (Exception e) {
        try {
                pm.currentTransaction().begin();
                a = new A();
                a.keyName = "A";
                pm.makePersistent(a);
                pm.currentTransaction().commit();
                System.out.println("New a was created");
        } finally {
                if (pm.currentTransaction().isActive())
                        pm.currentTransaction().rollback();
        }
}
System.out.println("id of a is " + KeyFactory.stringToKey(a.key));

// 2) get A from DB and add B to its list
try {
        pm.currentTransaction().begin();
        A newA = pm.getObjectById(A.class, "A");
        b = new B();

        b.keyName = "B";
        newA.bList.add(b);

        pm.currentTransaction().commit();
        System.out.println("New b was created");
} finally {
        if (pm.currentTransaction().isActive())
                pm.currentTransaction().rollback();
}
System.out.println("Key of b " + KeyFactory.stringToKey(b.key));

// 3) get B from DB and add C to its list
pm = PMF.get().getPersistenceManager();
try {
        pm.currentTransaction().begin();
        Key bKey = new KeyFactory.Builder(A.class.getSimpleName(), "A")
                        .addChild(B.class.getSimpleName(), "B").getKey();

        B newB = pm.getObjectById(B.class, bKey);
        c = new C();

        c.keyName = "C";
        newB.cList.add(c);

        pm.currentTransaction().commit();
        System.out.println("New c was created");
} finally {
        if (pm.currentTransaction().isActive())
                pm.currentTransaction().rollback();
}
System.out.println("Key of c " + KeyFactory.stringToKey(c.key));

// 4) delete B and C from DB
try {
        pm.currentTransaction().begin();
        C newC = pm.getObjectById(C.class, c.key);
        pm.deletePersistent(newC);
        B newB = pm.getObjectById(B.class, b.key);
        pm.deletePersistent(newB);
        pm.currentTransaction().commit();
        System.out.println("b and c was deleted");
} finally {
        if (pm.currentTransaction().isActive())
                pm.currentTransaction().rollback();
}

What do u think ?

-- 
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