pm.makePersistent()上NullPointerException的原因

时间:2012-06-07 00:36:37

标签: google-app-engine nullpointerexception jdo junit4 datanucleus

我对此毫无希望。使用JDO使用Google App Engine建立简单的一对多关系。我有一个课程“基础”java.util.Set<ThatOtherClass>中有几种其他子课程。但是有一个令人困惑的部分:我有一个非常复杂的类,有很多方法,一些静态字段和方法,当它们添加到Set中的父类时表现良好。但是,当我尝试添加一个新的相关类时,它无法在JUnit 4测试中持续存在,使用所有本地数据存储帮助程序来设置环境(正常工作,使用其他类测试)。这是堆栈跟踪:

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)

异常是从我调用pm.makePersistent(parentObject)的行抛出,对象本身已正确初始化,因为我注意到只是简单地添加@Persistent(mappedBy = "foreign_key") private OtherClass otherClassObject;会使单元测试在该行上失败,将其删除让它再次通过。

我还试图重写另一个类(仍然失败),甚至只是复制粘贴工作子类的代码(这令人惊讶地不会失败)。这一切让我想知道:对于更复杂的子类,我可以在Set中使用它而不是另一个可能有什么不同?

是的,我跟随https://developers.google.com/appengine/docs/java/datastore/jdo/relationships获得JDO 2.3(拥有一对多)。

我还有一些可能导致问题的怀疑,其中一个是DataNucleus增强器的一些过时缓存(我仍然使用带有DataNucleus 1.1的旧版1.0插件)。此外,我不确定问题是在目标类还是父类中,但更复杂的子类使用相同的关系定义,这使我认为它是目标子类。无论关系在pm.makePersistent()调用时的价值是多少都是无关紧要的,因为无论其中的值是什么,都会发生异常 - 我尝试了很多次。如果它可能是一些脏缓存,如何清除它?我甚至找不到DataNucleus日志。

更简单的非工作子类看起来基本上是这样的,删除了每个数据,因为我需要确定问题出在哪里(并且没有太多帮助):

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

关系另一边的领域:

@Persistent(mappedBy = "parent")
private Set<TestChild> testChildren;

有人可以帮助我吗?我花了好几个小时:-(

1 个答案:

答案 0 :(得分:2)

与您发布的内容不完全相同但是此问题 http://code.google.com/p/datanucleus-appengine/issues/detail?id=165 有相同的例外,并在2011年9月的新插件(v2.0 +)中得到修复。