Thank you for your answer. I modified my code as page you suggested
described and it worked. here is the new version
my data classes are
@PersistenceCapable(identityType = IdentityType.APPLICATION)
class A {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public Key key;
@Persistent(mappedBy = "a")
@Element(dependent = "true")
public List<B> bList = new LinkedList<B>();
}
@PersistenceCapable(identityType = IdentityType.APPLICATION)
class B {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public Key key;
@Persistent(mappedBy = "b")
@Element(dependent = "true")
public List<C> cList = new LinkedList<C>();
@Persistent
@Extension(vendorName="datanucleus", key="gae.parent-pk",
value="true")
public Key aKey;
@Persistent
public A a;
}
@PersistenceCapable(identityType = IdentityType.APPLICATION)
class C {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public Key key;
@Persistent
@Extension(vendorName="datanucleus", key="gae.parent-pk",
value="true")
public Key bKey;
@Persistent
public B b;
}
and here is my application code
PersistenceManager pm = PMF.get().getPersistenceManager();
//1)Create A,B,C
A a = new A();
B b = new B();
C c = new C();
//2)Persist A
pm.currentTransaction().begin();
try {
pm.makePersistent(a);
pm.currentTransaction().commit();
} finally {
if(pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
}
//3)get A from DB and add B to its list
pm.currentTransaction().begin();
try {
A newA = pm.getObjectById(A.class,a.key);
b.aKey = newA.key;
newA.bList.add(b);
pm.currentTransaction().commit();
} finally {
if(pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
}
//4) get B from DB and add C to its list
pm.currentTransaction().begin();
try {
Key bKey = new
KeyFactory.Builder(A.class.getSimpleName(),
a.key.getId()).addChild(B.class.getSimpleName(),
b.key.getId()).getKey();
B newB = pm.getObjectById(B.class,bKey);
//B newB = pm.getObjectById(B.class,b.key);//we can not
retrieve
this object. why?
c.bKey = newB.key;
newB.cList.add(c);
pm.currentTransaction().commit();
} finally {
if(pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
}
so I can get child nodes with parent id + child id
On Apr 19, 11:12 am, Ian Marshall <[email protected]> wrote:
> Because A is the entity group parent of B, the key of an instance of B
> must contain information about its entity group parent instance of A.
>
> Instead of using b.key.getId() in
>
> // We cannot retrieve this object. Why?
> B newB = pm.getObjectById(B.class,b.key.getId());
>
> you might care to use the KeyFactory.Builder class as detailed in
>
> "http://code.google.com/intl/en/appengine/docs/java/datastore/
> creatinggettinganddeletingdata.html#Creating_and_Using_Keys"
>
> in order to build a key instance which contains information about both
> your B instance and its parental A instance.
>
> --
> 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
> athttp://groups.google.com/group/google-appengine-java?hl=en.
--
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.