Objectify无法找到数据存储与缓存上的实体
问题描述:
我想通过ID加载实体来检查它是否存在。如果我把下面返回null:Objectify无法找到数据存储与缓存上的实体
String idS = lat+lon+lang;
GeoVars response = ofy().load().type(GeoVars.class).id(idS).now(); //Always returns null
如果我关掉缓存它返回entitiy:
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);
}
答
事实证明,我忘了把ObjectifyFilter放到web.xml中。现在它运作良好。
你使用的是什么版本的Objectify,如果你把'now()'改成'safe()'会发生什么? – tx802
@ tx802我正在使用v5。我会尝试通知safe()会发生什么,但通常我需要创建空值(如果它不存在)。 – savante
您可以将创建代码放入'catch(NotFoundException)'块中。 – tx802