【发布时间】:2015-10-14 00:47:04
【问题描述】:
我正在尝试按 id 加载实体以检查它是否存在。如果我按以下方式调用,则返回 null:
String idS = lat+lon+lang;
GeoVars response = ofy().load().type(GeoVars.class).id(idS).now(); //Always returns null
如果我关闭缓存,它会返回实体:
GeoVars response = ofy().cache(false).load().type(GeoVars.class).id(idS).now(); //Returns OK
这里是 GeoVars 类:
@Entity
@Cache
public class GeoVars {
@Id String id;
String summary;
String something;
String another;
String blabla;
String lang;
int day;
我知道 memcache 是“尽力而为”的基础,但即使它没有被缓存,它也应该自动从数据存储中获取实体?为什么load会返回null?
编辑:
当我尝试使用安全时,也返回 null。
try {
response = ofy().load().type(GeoVars.class).id(idS).safe();
} catch (com.googlecode.objectify.NotFoundException e) {
e.printStackTrace();
response = createResponse(idS, lat, lon, lang, dayofweek);
ofy().save().entity(response);
}
【问题讨论】:
-
您使用的是什么版本的 Objectify,如果您将
now()更改为safe()会发生什么? -
@tx802 我正在使用 v5。我将尝试告知 safe() 会发生什么,但通常如果它不存在,我需要创建 null 值。
-
您可以将创建代码放在
catch(NotFoundException)块中。 -
我试过了,同样的事情发生了。你可以看到上面的编辑。
-
@tx802 有什么想法吗?我真的被困住了!
标签: java android google-app-engine caching objectify