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