I have a GAE JDO annotated object:
@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.
When I create one using pm.makePersistent(helloGreeting);
, then the api explorer shows me something like this:
{
"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:
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());
in my update(HelloGreeting helloGreeting)
method.
It seems like the private Key id field is not unmarshalled correctly. I tried it also with 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
.
我不确定您的情况,但我认为您可能需要编写自定义转换器来确保正确重构密钥。
I solved this with a bit nasty solution. This is my update()
endpoint:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.