[英]How to create the com.google.appengine.api.datastore.Key object in the Google App Engine _ah/api/explorer?
I have a GAE JDO annotated object: 我有一个带有GAE JDO注释的对象:
@PersistenceCapable
public class HelloGreeting {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;
@Persistent
private String message;
... constructors, getters, setters
}
and my own endpoint: 和我自己的端点:
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;
}
where I would like to send from frontend or from the Google App Engine _ah/api/explorer the helloGreeting object. 我想从前端或Google App Engine _ah / api / explorer 发送helloGreeting对象的位置。
When I create one using pm.makePersistent(helloGreeting);
当我使用
pm.makePersistent(helloGreeting);
创建一个 , then the api explorer shows me something like this: ,然后api资源管理器向我显示了以下内容:
{
"id": {
"kind": "HelloGreeting",
"appId": "project-id",
"id": "5629499534213120",
"complete": true
},
"message": "some text"
}
I use then this JSON structure to call the update(HelloGreeting helloGreeting)
method via the api explorer and I'm getting the following error: 然后,我使用此JSON结构通过api资源管理器调用
update(HelloGreeting helloGreeting)
方法,但出现以下错误:
java.lang.NullPointerException at com.google.appengine.api.datastore.Key.getAppId(Key.java:279)
...
...
... which was because of line pm.getObjectById(HelloGreeting.class, helloGreeting.getId());
...这是由于
pm.getObjectById(HelloGreeting.class, helloGreeting.getId());
in my update(HelloGreeting helloGreeting)
method. 在我的
update(HelloGreeting helloGreeting)
方法中。
It seems like the private Key id field is not unmarshalled correctly. 似乎私钥ID字段未正确解组。 I tried it also with
pm.getObjectById(HelloGreeting.class, helloGreeting.getId().getId());
我也尝试过
pm.getObjectById(HelloGreeting.class, helloGreeting.getId().getId());
but with no success. 但没有成功。
Can somebody please tell me if this is even possible? 有人可以告诉我这是否可能吗?
PS: I can't change the type of the id field from Key to Long (which works) because then I wouldn't use inner child object annotated with @PersistenceCapable
. PS:我无法将id字段的类型从Key更改为Long (这是可行的),因为那样的话我就不会使用带有
@PersistenceCapable
注释的内部子对象。
我不确定您的情况,但我认为您可能需要编写自定义转换器来确保正确重构密钥。
I solved this with a bit nasty solution. 我用一个讨厌的解决方案解决了这个问题。 This is my
update()
endpoint: 这是我的
update()
端点:
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;
}
If somebody knows a better solution, I would appreciate it. 如果有人知道更好的解决方案,我将不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.