Hi every body. I have a problem with my model and I cannot figure out
what the problem is. So any help would be appreciated. to explain the
problem I wrote some sample code:
Here is my model:
Class A has one to many relationship with B
Class B has one to many relationship with C
@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
public A a;
}
@PersistenceCapable(identityType = IdentityType.APPLICATION)
class C {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public Key key;
@Persistent
public B b;
}
in my code;
1) I created class A B and C
2) I persisted A
3) I retrieved A from DB and add B to its list.
4) I retrieved B from DB and add C to its list.
but in step 4, I cannot retrieve B from DB, instead I get following
exception
javax.jdo.JDOObjectNotFoundException: Could not retrieve entity of
kind B with key B(4).
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.getId());
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 {
/********************Error*********************/
B newB = pm.getObjectById(B.class,b.key.getId());//we cannot
retrieve this object. why?
newB.cList.add(c);
pm.currentTransaction().commit();
} finally {
if(pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
}
Please note that I am not sure that this is how I suppose to implement
owned one to many relationship I got that implementation from
following post
http://groups.google.com/group/google-appengine-java/browse_thread/thread/28d9c3b99df25e43/b43702a150c5aa1b?lnk=gst&q=owned+one+to+many+getObjectById#b43702a150c5aa1b
--
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.