While upgrading Objectify in an Appengine Java-based application, I got stuck with a problem and here is a solution, I have tried to solve this but not sure how good it is?
The problem was In the older version (Objectify 4.0b2), a query returned a Reference to the DB object.
Ref<EntityDO> refEntity = ofy().load().type(EntityDO.class).id(entityId);
This used to be a valid code as ofy().load().type().id() used to return a reference to the DB entity.
After upgrading Objectify to a later version, this has changed to
LoadResult<EntityDO> entityDO = ofy().load().type(EntityDO.class).id(entityId);
Now I used this code to convert the LoadResult<> object to Ref<> object.
public static <T> Ref<T> getRef(LoadResult<T> loadResult) {
if (loadResult != null) {
T obj = loadResult.now();
if (obj != null) {
return Ref.create(obj);
}
}
return null;
}
With this, I am able to get the Ref<> object successfully. My concern here is is this a good approach where to create a Ref<> object, we fetch the object from DB?
Thanks Aadhaar
It's a bit peculiar. You could just call this, which is equivalent:
Ref.create(Key.create(EntityDO.class, entityId))
...but I'm guessing it's semantically not what you expect? I can't honestly remember exactly what 4.0b2 did (7 years ago), but at some point Objectify's Ref
changed from being an object that held a direct object reference to an entity, to just being a holder of a Key
with the ability to look up the value in the current session.
Without knowing more about your app it's hard to offer much advice. But if you just need a Ref
, create it from a Key
.