I'm trying to return an entity (Room) from Google App Engine datastore, using endpoints. This method (auto-generated) returns all entities in the datastore:
@SuppressWarnings({ "unchecked", "unused" })
public CollectionResponse<Room> listRoom(
@Nullable @Named("cursor") String cursorString,
@Nullable @Named("limit") Integer limit) {
EntityManager mgr = null;
Cursor cursor = null;
List<Room> execute = null;
try {
mgr = getEntityManager();
Query query = mgr.createQuery("select from Room as Room");
if (cursorString != null && cursorString != "") {
cursor = Cursor.fromWebSafeString(cursorString);
query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
}
if (limit != null) {
query.setFirstResult(0);
query.setMaxResults(limit);
}
execute = (List<Room>) query.getResultList();
cursor = JPACursorHelper.getCursor(execute);
if (cursor != null)
cursorString = cursor.toWebSafeString();
// Tight loop for fetching all entities from datastore and accomodate
// for lazy fetch.
for (Room obj : execute)
;
} finally {
mgr.close();
}
return CollectionResponse.<Room> builder().setItems(execute)
.setNextPageToken(cursorString).build();
}
I want to edit this so it only returns one entity based on a property, a string. So I'll pass the string in as a parameter, find it in the datastore and return it. The string will not be the primary key.
EDIT:
Trying this but still doesn't work:
public Room getRoom(@Named("id") String mac) {
EntityManager mgr = null;
Room room = null;
try {
mgr = getEntityManager();
Query query = mgr.createQuery("SELECT * FROM Room WHERE mac_adds IN ('"+mac+"')");
room = (Room) query.getSingleResult();
} finally {
mgr.close();
}
return room;
}
Any help appreciated.
Thanks,