GAE EntityManager发现抛出null

时间:2014-03-04 13:26:05

标签: google-app-engine jpa google-cloud-datastore

我一直在玩GAE Java后端,我正在尝试使用实体管理器来更新给定密钥的实体。首先,我需要使用实体管理器中的.find方法从数据存储区中查找并获取实体。但是.find方法不断抛出空指针错误,即使我知道实体存在(我在数据存储区查看器中看到它)。有人知道什么是错的吗?感谢。

该实体定义为:

@PersistenceCapable(detachable = "true")
public class Event {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
private String name;
private String description;

这是代码失败的地方:

@ApiMethod(name = "updateEvent", path="update_event")
public Event updateEvent(Event event) {
    EntityManager mgr = getEntityManager();
    if( mgr==null || event==null || event.getKey()==null)
        return null;
    try {
        Event item = mgr.find(Event.class, event.getKey()); // java.lang.NullPointerException thrown here

这是堆栈跟踪:

com.clearscoreboard.EventEndpoint updateEvent: Stacktrace: java.lang.NullPointerException
at com.google.appengine.api.datastore.KeyTranslator.convertToPb(KeyTranslator.java:49)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$V3KeyBatcher.toPb(AsyncDatastoreServiceImpl.java:111)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$V3KeyBatcher.toPb(AsyncDatastoreServiceImpl.java:102)
at com.google.appengine.api.datastore.Batcher$BatchIterator.<init>(Batcher.java:176)
at com.google.appengine.api.datastore.Batcher$1.<init>(Batcher.java:270)
at com.google.appengine.api.datastore.Batcher.getBatches(Batcher.java:270)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.doBatchGet(AsyncDatastoreServiceImpl.java:301)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.get(AsyncDatastoreServiceImpl.java:280)
at com.google.appengine.api.datastore.DatastoreServiceImpl$1.runInternal(DatastoreServiceImpl.java:69)
at com.google.appengine.api.datastore.DatastoreServiceImpl$1.runInternal(DatastoreServiceImpl.java:66)
at com.google.appengine.api.datastore.TransactionRunner.runInTransaction(TransactionRunner.java:29)
at com.google.appengine.api.datastore.DatastoreServiceImpl.get(DatastoreServiceImpl.java:66)
at com.google.appengine.api.datastore.DatastoreServiceImpl.get(DatastoreServiceImpl.java:56)
at com.google.appengine.datanucleus.WrappedDatastoreService.get(WrappedDatastoreService.java:60)
at com.google.appengine.datanucleus.EntityUtils.getEntityFromDatastore(EntityUtils.java:665)
at com.google.appengine.datanucleus.DatastorePersistenceHandler.fetchObject(DatastorePersistenceHandler.java:543)
at org.datanucleus.state.JDOStateManager.loadFieldsFromDatastore(JDOStateManager.java:1638)
at org.datanucleus.state.JDOStateManager.validate(JDOStateManager.java:3511)
at org.datanucleus.ObjectManagerImpl.findObject(ObjectManagerImpl.java:3379)
at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:358)
at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:256)
at com.clearscoreboard.EventEndpoint.updateEvent(EventEndpoint.java:240)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_$1.run(Method_.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_.privilegedInvoke(Method_.java:176)
at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_.invoke_(Method_.java:137)
at com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_.invoke(Method_.java:45)
at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:359)
at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:124)
at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:82)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:266)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:446)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:188)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
at java.lang.Thread.run(Thread.java:724)

1 个答案:

答案 0 :(得分:0)

检查getEntityManager方法。 如果没有结果尝试这样的事情(100%工作):

public T get(Key key) throws Exception {
    EntityManager mgr = EMF.get().createEntityManager();
    T entity = null;
    try {
        entity = mgr.find(entityClass, key);
    } finally {
        mgr.close();
    }
    if (entity == null) {
        throw DS_NOT_FOUND();
    }
    return entity;
}

public final class EMF {
private static final EntityManagerFactory emfInstance = Persistence.createEntityManagerFactory("transactions-optional");

private EMF() {
}

public static EntityManagerFactory get() {
    return emfInstance;
}

这肯定会有用。