0
votes

I have a UserAccount that has a list of followings. This list of followings is saved as its own entity. I usually don't need the following list and, thus, I don't use @Load. However, I have a situation where I would like to load multiple UserAccounts together with their followings.

So, I need something like this:

OfyService.ofy().load().type(UserAccount.class).limit(200).loadReference("followerRef").list();

Example for the UserAccount:

@Entity
@Cache
public class UserAccount {
    @Id private String email;
    @Index
    private String nickname;
    private Ref<UserFollowing> followingRef;
}
1

1 Answers

2
votes

I believe Objectify already provides a way to do what you are after in the form of Load Groups. As you rightly pointed out using the @Load annotation will automatically retrieve a UserAccounts's followingRef which you don't want to happen for most cases.

In order to load a list of UserAccount with their followingRef you first have to apply the following simple modifications to your entity:

@Entity
public class UserAccount {
    public static class Everything {}

    @Id Long id;
    @Load(Everything.class) Ref<UserFollowing> followingRef;
}

You can then do the following to load a single UserAccount object:

// Doesn't load followingRef
UserAccount ua = ofy().load().key(userAccountKey).now();

// Does load followingRef
UserAccount ua = ofy().load().group(Everything.class).key(userAccountKey).now();

Similary you do this to load a list of UserAccount objects with their followinfRef:

// In your case you'd do something like
List<UserAccount> accounts = ofy().load()
                                  .type(UserAccount.class)
                                  .group(Everything.class)
                                  .list();

That should hopefully solve your problem. I don't know if that code compiles but if it doesn't shouldn't be too hard to fix.

For further reading click here and scroll down to Load Groups section