0
我想通過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);
}
你使用的是什麼版本的Objectify,如果你把'now()'改成'safe()'會發生什麼? – tx802
@ tx802我正在使用v5。我會嘗試通知safe()會發生什麼,但通常我需要創建空值(如果它不存在)。 – savante
您可以將創建代碼放入'catch(NotFoundException)'塊中。 – tx802