Java 如何在google App Engine\u ah/api/explorer中创建com.google.appengine.api.datastore.Key对象?
我有一个GAE JDO注释对象:Java 如何在google App Engine\u ah/api/explorer中创建com.google.appengine.api.datastore.Key对象?,java,google-app-engine,google-cloud-endpoints,jdo,Java,Google App Engine,Google Cloud Endpoints,Jdo,我有一个GAE JDO注释对象: @PersistenceCapable public class HelloGreeting { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Key id; @Persistent private String message; ... constructors, getters, setters } 还有我自己的终点:
@PersistenceCapable
public class HelloGreeting {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;
@Persistent
private String message;
... constructors, getters, setters
}
还有我自己的终点:
public HelloGreeting update(HelloGreeting helloGreeting)
throws NotFoundException {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.getObjectById(HelloGreeting.class, helloGreeting.getId());
... some logic
} finally {
pm.close();
}
return helloGreeting;
}
public HelloGreeting update(HelloGreeting helloGreeting)
throws NotFoundException {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
if(!containsGreeting(helloGreeting)) {
throw new NotFoundException("not exists");
}
// persisting the helloGreeting with the new Key
pm.makePersistent(helloGreeting);
} finally {
pm.close();
}
return helloGreeting;
}
private boolean containsGreeting(HelloGreeting helloGreeting) {
PersistenceManager pm = PMF.get().getPersistenceManager();
boolean contains = true;
try {
// here I'm creating a new Key from the helloGreeting
Key key = KeyFactory.createKey(HelloGreeting.class.getSimpleName(), helloGreeting.getId().getId());
// using the Key for a query
pm.getObjectById(HelloGreeting.class, key);
// and rewriting the old incorrect unmarshalled Key by the new one
helloGreeting.setId(key);
} catch (javax.jdo.JDOObjectNotFoundException ex) {
contains = false;
} finally {
pm.close();
}
return contains;
}
我想从前端或谷歌应用程序引擎发送helloGreeting对象
当我使用pm.makePersistenthelloGreeting;创建一个时;,然后api资源管理器向我显示如下内容:
{
"id": {
"kind": "HelloGreeting",
"appId": "project-id",
"id": "5629499534213120",
"complete": true
},
"message": "some text"
}
然后,我使用此JSON结构通过api资源管理器调用updateHelloGreeting helloGreeting方法,得到以下错误:
java.lang.NullPointerException at com.google.appengine.api.datastore.Key.getAppId(Key.java:279)
...
...
。。。这是因为行pm.getObjectByIdHelloGreeting.class,helloGreeting.getId;在我的updateHelloGreeting helloGreeting方法中
私钥id字段似乎未正确解组。
我还用pm.getObjectByIdHelloGreeting.class、helloGreeting.getId.getId尝试了它;但是没有成功
谁能告诉我这是否可能
PS:我不能将id字段的类型从Key改为Long,因为这样我就不会使用带有@PersistenceCapable注释的内部子对象。我对您的情况不是100%确定,但我认为您可能需要编写一个自定义项来确保正确地重构密钥。我用一个有点讨厌的解决方案解决了这个问题。这是我的更新端点:
public HelloGreeting update(HelloGreeting helloGreeting)
throws NotFoundException {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.getObjectById(HelloGreeting.class, helloGreeting.getId());
... some logic
} finally {
pm.close();
}
return helloGreeting;
}
public HelloGreeting update(HelloGreeting helloGreeting)
throws NotFoundException {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
if(!containsGreeting(helloGreeting)) {
throw new NotFoundException("not exists");
}
// persisting the helloGreeting with the new Key
pm.makePersistent(helloGreeting);
} finally {
pm.close();
}
return helloGreeting;
}
private boolean containsGreeting(HelloGreeting helloGreeting) {
PersistenceManager pm = PMF.get().getPersistenceManager();
boolean contains = true;
try {
// here I'm creating a new Key from the helloGreeting
Key key = KeyFactory.createKey(HelloGreeting.class.getSimpleName(), helloGreeting.getId().getId());
// using the Key for a query
pm.getObjectById(HelloGreeting.class, key);
// and rewriting the old incorrect unmarshalled Key by the new one
helloGreeting.setId(key);
} catch (javax.jdo.JDOObjectNotFoundException ex) {
contains = false;
} finally {
pm.close();
}
return contains;
}
如果有人知道更好的解决方案,我将不胜感激。我害怕这个答案:D