Google App Engine – Querying related table using Key object
This is just a short note on how to query related entities in JDO. As I have found out, we cannot use the entity key as its shown in the Data Store Viewer on the GAE admin console eg. zzR0ZXN0cgkLEgNGb28XGQw and compare that using someRelatedEntityKey == zR0ZXN0cgkLEgNGb28XGQw, instead we just provide the whole Key object to the query using query parameters.
Eg. Select users belonging to a department:
String queryStr = "SELECT FROM " + User.class.getName() + " WHERE departmentKey = :paramOne"
//and then:
List<User> users = (List<User>) pm.newQuery(queryStr).execute(departmetnKey);
//and then:
List<User> users = (List<User>) pm.newQuery(queryStr).execute(departmetnKey);
Where pm is an instance of PersistenceManager, departmentKey is a Key object to the Department entity and paramOne is a named query parameter.
One response on “Google App Engine – Querying related table using Key object”