I’m getting hopeless on this. Modelling a simple one-to-many relationships with Google App Engine with JDO. I have a class that “has” a few kinds of other child classes in basic java.util.Set<ThatOtherClass>. But there comes the confusing part: I have one pretty complex class with a lot of methods, some static fields and methods, which behaves well when added to the parent class in a Set. But, then, when I try to add a new related class, it just fails to persist in a JUnit 4 test, using all the local datastore helpers to set the environment (working properly, tested with other classes). This is the stack trace:
java.lang.NullPointerException
at org.datanucleus.store.mapped.mapping.PersistenceCapableMapping.postInsert(PersistenceCapableMapping.java:1039)
at org.datanucleus.store.appengine.DatastoreRelationFieldManager.runPostInsertMappingCallbacks(DatastoreRelationFieldManager.java:218)
at org.datanucleus.store.appengine.DatastoreRelationFieldManager.access$200(DatastoreRelationFieldManager.java:49)
at org.datanucleus.store.appengine.DatastoreRelationFieldManager$1.apply(DatastoreRelationFieldManager.java:117)
at org.datanucleus.store.appengine.DatastoreRelationFieldManager.storeRelations(DatastoreRelationFieldManager.java:82)
at org.datanucleus.store.appengine.DatastoreFieldManager.storeRelations(DatastoreFieldManager.java:959)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.storeRelations(DatastorePersistenceHandler.java:585)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertPostProcess(DatastorePersistenceHandler.java:320)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObjects(DatastorePersistenceHandler.java:272)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObject(DatastorePersistenceHandler.java:256)
at org.datanucleus.state.JDOStateManagerImpl.internalMakePersistent(JDOStateManagerImpl.java:3185)
at org.datanucleus.state.JDOStateManagerImpl.makePersistent(JDOStateManagerImpl.java:3161)
at org.datanucleus.ObjectManagerImpl.persistObjectInternal(ObjectManagerImpl.java:1298)
at org.datanucleus.ObjectManagerImpl.persistObject(ObjectManagerImpl.java:1175)
at org.datanucleus.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:669)
at org.datanucleus.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:694)
at mypackage.MyTests.myTest(MyTests.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
The exception is getting thrown from the line where I call pm.makePersistent(parentObject), the object itself is properly initialized, as I notice that only simply adding @Persistent(mappedBy = "foreign_key") private OtherClass otherClassObject; makes the unit test fail on that line, removing it makes it pass again.
I also tried to rewrite the other class (still fails), and even just copy-paste the code of the working child class (which surprisingly doesn’t fail). That all makes me wonder: what could possibly be different about the more complex child class that I can use it in a Set and not the other one?
Yes, I followed https://developers.google.com/appengine/docs/java/datastore/jdo/relationships for JDO 2.3 (owned one-to-many).
I have just a few more suspicions left what could cause the problem, one of which is some stale cache of the DataNucleus enhancer (I still use the older 1.0 plugin with DataNucleus 1.1). Moreover, I am not sure whether the problem is in the target class or the parent class, but the more complex child class works with the very same relationship definition, so that makes me think it’s the target child class. Doesn’t matter what the value of the relationship is at the pm.makePersistent() call time, as the exception happens no matter what the value is in it – I tried it a lot of times. If it could be some dirty cache, how to clear it? I couldn’t even find the DataNucleus logs.
The simpler non-working child class looks basically like this, stripped of every data as I needed to be sure where is the problem (and didn’t help myself much):
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;
import com.google.appengine.api.datastore.Key;
@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Version(strategy = VersionStrategy.VERSION_NUMBER)
public class TestChild
{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;
@Persistent
private Parent parent;
public void setParent(Person parent)
{
this.parent = parent;
}
public Parent getParent()
{
return this.parent;
}
}
And the field on the other side of the relationship:
@Persistent(mappedBy = "parent")
private Set<TestChild> testChildren;
Can anybody help me, please? I spent hours on this 🙁
Not the exact same as what you posted but this issue
http://code.google.com/p/datanucleus-appengine/issues/detail?id=165
has the same exception and was fixed in the new plugin (v2.0+) in Sept 2011.