Lukas Knuth Lukas Knuth - 1 year ago 70
Java Question

Eager-Load all entries of a List<Ref<T>>

I currently use this pattern to store lists of types that are also Objectify entities:

class Test{
private List<Ref<T>> data = new ArrayList<>();

public List<T> getTees(){
List<T> tmp = new ArrayList<>(; // Is the entire list loaded here?
for (Ref<T> d : {
tmp.add(d.get()); // or every single entry here??
return tmp;

The question is: will the first time the
-field is used load the entire list (the data that
"points" to) from the data-storage, or will the
-method load each entry individually?

If so, how can a bulk-load the entire list at once?

Note: I don't always need the list, that's why it doesn't have the

Answer Source

Objectify will not load anything referenced unless you .get() it explicitly, this results in individual fetches, or you tell it to auto load all the things via @Load. That speeds up loading overall because it can batch process the request instead of forming several individual ones.

If by "I don't always need the list" you mean that you have distinct cases of "need" vs "not need", take a look at load groups, essentially a way to turn the @Load annotation on or off. has examples like yours to turn on/off auto loading

There is btw a way to load a list of Refs in a single batch using
Map<Key<T>,T> Loader#refs(Iterable<Ref<T>> refs)

public List<T> getTees(){
    Collection<T> values = ofy().load().refs(data).values();
    return new ArrayList<T>(values);