我正在尝试使用端点从Google App Engine数据存储区返回实体(Room)。此方法(自动生成)返回数据存储区中的所有实体:
@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;
}
任何帮助表示感谢。
谢谢,
答案 0 :(得分:2)
您可以尝试替换:
for (Room obj : execute)
;
与
for (Room obj : execute) {
if( obj.your_property.equal(your_string) ) {
// do something with new found `obj`, maybe return it
}
}
你也很可能不需要在函数末尾返回CollectionResponse
。
答案 1 :(得分:0)
我有一个类似的问题,我解决它的方式如下:
我在端点类中创建了一个新的API方法。在我的情况下,我不得不从userTable实体类中获取与特定电子邮件地址相对应的行。所以我将emailAddress作为String参数传递给我在端点类中添加的API方法。我在下面的示例emaiAddress不是我的实体的主键。
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "getUserTableByEmail")
public CollectionResponse<UserTable> getUserTableByEmail(
@Nullable @Named("cursor") String cursorString,
@Nullable @Named("limit") Integer limit,
@Named ("emailAddress") String email) {
EntityManager mgr = null;
Cursor cursor = null;
List<UserTable> execute = null;
try {
mgr = getEntityManager();
Query query = mgr.createQuery("select n from UserTable n where n.emailAddress = '"+email+"'");
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<UserTable>) query.getResultList();
cursor = JPACursorHelper.getCursor(execute);
if (cursor != null)
cursorString = cursor.toWebSafeString();
for (UserTable obj : execute)
;
} finally {
mgr.close();
}
return CollectionResponse.<UserTable> builder().setItems(execute)
.setNextPageToken(cursorString).build();
}
但请注意一件事。我仍然使用该方法返回一个CollectionResponse对象,如果您确定只有一行返回了您的选择条件,则可以返回一个userTable(实体)对象。
我的android代码,我用户CollectionResponse.isEmpty()检查是否有任何返回,如果它返回行然后我使用方法CollectionResponse.getItem()来检索行。
如果您希望查询只返回一个结果,那么您可以在Android代码中执行端点方法时将Integer参数“limit”指定为1或者只是放入
query.setMaxResults(1);
在上面的方法中。