Hi.

I got tree classes that has one-to-many relationship with each other.
A has a child class B (one to many)
B has a child class C(one to many)

@PersistenceCapable
class A {
    @PrimaryKey
    @Persistent
    public Key key;
    @Persistent(mappedBy = "a")
    @Element(dependent = "true")
    public List<B> bList = new ArrayList<B>();
}

@PersistenceCapable
class B {
    @PrimaryKey
    @Persistent
    public Key key;
    @Persistent(mappedBy = "b")
    @Element(dependent = "true")
    public List<C> cList = new ArrayList<C>();
    @Persistent
    public A a;
}

@PersistenceCapable
class C {
    @PrimaryKey
    @Persistent
    public Key key;
    @Persistent
    public B b;
}

and here is my sample code. code does following steps
1)Create an A instance and persist it if it does not exist in
datastore
2)get A from DB and add B to its list
3) get B from DB and add C to its list
4) delete B and C from DB

                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.key = new
KeyFactory.Builder(A.class.getSimpleName(),"A").getKey();
                                pm.makePersistent(a);
                    pm.currentTransaction().commit();
                    log.info("New a was created");
                        } finally {
                        if(pm.currentTransaction().isActive())
                                pm.currentTransaction().rollback();
                }
                }
        log.info("id of a is " + 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.key = new
KeyFactory.Builder(newA.key).addChild(B.class.getSimpleName(),
"B").getKey();
                        newA.bList.add(b);
            pm.currentTransaction().commit();
            log.info("New b was created");
                } finally {
                        if(pm.currentTransaction().isActive())
                                pm.currentTransaction().rollback();
                }
                log.info("Key of b " + b.key);
        //3) get B from DB and add C to its list
                pm.currentTransaction().begin();
                try {
                        Key bKey  = new 
KeyFactory.Builder(A.class.getSimpleName(),
"A").addChild(B.class.getSimpleName(), "B").getKey();
                        B newB = 
pm.getObjectById(B.class,bKey);//<----------Error we
cannot retrieve B
                        c = new C();
                        c.key = new
KeyFactory.Builder(newB.key).addChild(C.class.getSimpleName(),
"C").getKey();
                        newB.cList.add(c);
                        pm.currentTransaction().commit();
            log.info("New c was created");
                } finally {
                if(pm.currentTransaction().isActive())
                        pm.currentTransaction().rollback();
        }
                //4) delete B and C from DB
        pm.currentTransaction().begin();
                try {
                        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();
            log.info("b and c was deleted");
                } finally {
                if(pm.currentTransaction().isActive())
                        pm.currentTransaction().rollback();
        }

first time I run this code it works. here is my log messages

INFO: New a was created
INFO: id of a is A("A")
INFO: New b was created
INFO: Key of b A("A")/B("B")
INFO: New c was created
INFO: b and c was deleted

it creates a new A , B and C then in step 4 it deletes B and C
when I run it second time I get following an error message

javax.jdo.JDOObjectNotFoundException: Could not retrieve entity of
kind B with key A("A")/B("B")

Problem is, if my parent(A) is already in datastore I cannot add a new
child(B).
I dont get any error at step2. It seems like B is persisted correctly
but when I try to retrive it at step 3( with code
"pm.getObjectById(B.class,bKey)" at line 3 of this step)
I cannot retrieve it. and when I check it from admin console I see
that it is not persisted.

I found this post which says that I have to assign primary keys by
hand but it still does not work.
http://groups.google.com/group/google-appengine-java/browse_thread/thread/22e808b07fd93062/e81ec52c4fee8103?lnk=gst&q=owned+one+to+many#e81ec52c4fee8103

any help would be appreciated. thanks

-- 
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 google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to