简体   繁体   中英

retrieve an entity with a key from datastore

I am trying to retrieve an entity from my database using its id. I've tried using all kind of commands but the result stays the same , I always get an empty entity. I know the id is correct because I run a query on my database and receive this id.
this is how i receive the id from my database

Object course = courseList.get(0).getKey().getId();

I tried creating a key from that id and use

Key key = KeyFactory.createKey("courses", course);
db.get(key);    

even tried run a query on database with that key.I can see the the query is correct but I still receive an empty entity:

Query query = new Query("Courses").setFilter(Query.FilterOperator.EQUAL.of(Entity.KEY_RESERVED_PROPERTY, key));    

and also like that

Query query = new Query("Courses").setFilter(Query.FilterOperator.EQUAL.of("__Key__", key));    

I'm out of ideas ... what am I doing wrong? I know for sure this key exist.In debug mode I can see this key.I see it also in my database. thanks

I never played with Java too much but from the docs something like this should work:

PersistenceManager pm = PMF.get().getPersistenceManager();
Courses c = pm.getObjectById(Cources.class, course);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM