Getting an element by id in Google App Engine's JDO
I have a problem when trying to find a single element by id. The entity class is this:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Test{
@PrimaryKey
private String name;
//..and so on.
In this case, the value "name" is acting like an id. I'm using this query to find the element:
PersistenceManager pm = PMF.get().getPersistenceManager();
Query q = pm.newQuery(Test.class);
q.setFilter("name == " + id);
//String query = "SELECT FROM " + Test.class.getName() + "WHERE name == " + id;
Test test = (Test) q.execute();
return category;
Assuming id is a string value 开发者_如何学Csent as a parameter by other method.
But it doesn't retrieve any data. Also, it throws an exception: java.lang.ClassCastException: org.datanucleus.store.appengine.query.StreamingQueryResult cannot be cast to com.santiagopoli.gapptest.domain.Test
The commented query also doesn't work.
I hope anyone can help me. Is a shame that something that basic is difficult to achieve. In other sql implementations, it will be easy as typing "select * from Test where id=". Thanks!
I just fixed it.
Try this...
Test t = pm.getObjectById(Test.class, id);
reference... http://code.google.com/intl/us/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata.html
精彩评论