我正在尝试使用端点从 Google App Engine 数据存储区返回一个实体(房间)。此方法(自动生成)返回数据存储区中的所有实体:
@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();
}
我想编辑它,使它只返回一个基于属性的实体,一个字符串。所以我将把字符串作为参数传入,在数据存储中找到它并返回它。该字符串不会是主键。
编辑:
尝试这个但仍然不起作用:
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;
}
任何帮助表示赞赏。
谢谢,