在Android应用程序的GAE(Android后端)中更新实体(包含子对象列表的对象)时,我是android和gae以及面临问题的新手
情景是:
我已经在数据源中保存了父对象。然后我获取此对象并添加子对象列表(新创建),然后触发更新。我的实体是:
父对象
public class ParentObject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private String placeId;
@OneToOne(cascade = CascadeType.ALL)
private List<ChildObject> childObjects;
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
public String getPlaceId() {
return placeId;
}
public void setPlaceId(String placeId) {
this.placeId = placeId;
}
public List<ChildObject> getChildObject() {
return childObjects;
}
public void setChildObject(List<ChildObject> childObjects) {
this.childObjects = childObjects;
}
}
子对象
@Entity
public class ChildObject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
private String userEmail;
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
}
更新AsyncTask代码
@Override
protected ParentObject doInBackground(ParentObject... params) {
ParentObject parentObj = params[0];
Parentobjectendpoint.Builder builder = new Parentobjectendpoint.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
null);
builder = CloudEndpointUtils.updateBuilder(builder);
Parentobjectendpoint endpoint = builder.build();
ParentObject updatedObj;
try {
updatedObj = endpoint.updateParentObject(parentObj).execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
updatedObj = null;
}
return updatedObj;
}
我得到的错误是
com.google.api.server.spi.SystemService invokeServiceMethod: null
java.lang.NullPointerException
at com.google.appengine.api.datastore.Key.getAppId(Key.java:279)
at com.google.appengine.api.datastore.KeyTranslator.convertToPb(KeyTranslator.java:50)
at com.google.appengine.api.datastore.EntityTranslator.convertToPb(EntityTranslator.java:48)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:178)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:155)
at com.google.appengine.api.datastore.Batcher$BatchIterator.<init>(Batcher.java:180)
at com.google.appengine.api.datastore.Batcher$2.<init>(Batcher.java:317)
at com.google.appengine.api.datastore.Batcher.getBatches(Batcher.java:317)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.doBatchPut(AsyncDatastoreServiceImpl.java:365)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:293)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:239)
at com.google.appengine.api.datastore.DatastoreServiceImpl.put(DatastoreServiceImpl.java:61)
at com.google.appengine.datanucleus.WrappedDatastoreService.put(WrappedDatastoreService.java:112)
at com.google.appengine.datanucleus.EntityUtils.putEntitiesIntoDatastore(EntityUtils.java:766)
at com.google.appengine.datanucleus.DatastorePersistenceHandler.insertObjectsInternal(DatastorePersistenceHandler.java:314)
at com.google.appengine.datanucleus.DatastorePersistenceHandler.insertObject(DatastorePersistenceHandler.java:218)
at org.datanucleus.state.JDOStateManager.internalMakePersistent(JDOStateManager.java:2381)
at org.datanucleus.state.JDOStateManager.flush(JDOStateManager.java:3778)
at org.datanucleus.ObjectManagerImpl.flushInternalWithOrdering(ObjectManagerImpl.java:3888)
at org.datanucleus.ObjectManagerImpl.flushInternal(ObjectManagerImpl.java:3811)
at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:3751)
at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:4141)
at org.datanucleus.ObjectManagerImpl.transactionPreCommit(ObjectManagerImpl.java:428)
at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:398)
at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:287)
at org.datanucleus.ObjectManagerImpl.close(ObjectManagerImpl.java:1090)
at org.datanucleus.api.jpa.JPAEntityManager.close(JPAEntityManager.java:193)
at com.sandeepapplabs.dms.VehicleEndpoint.updateVehicle(VehicleEndpoint.java:130)
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:45)
at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:359)
at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:160)
at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:118)
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 org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
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 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 org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:438)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:445)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:220)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:309)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:301)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:442)
at java.lang.Thread.run(Thread.java:724)
答案 0 :(得分:0)
好的,我可以通过将ParentObject的主键更改为
来解决问题@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
对类似问题的大量研究帮助我解决了这个问题