0
votes

I have a problem Ref<> usage with @Load. Basically I made a copy paste from Objectify website to test @Load annotation with Load Groups.

@Entity
public static class Thing {
    public static class Partial {}
    public static class Everything extends Partial {}
    public static class Stopper {}

    @Id Long id;
    @Load(Partial.class) Ref<Other> withPartial;
    @Load(Everything.class) Ref<Other> withEveryhthing;
    @Load(unless=Stopper.class) Ref<Other> unlessStopper;

    public Ref<Other> getWithPartial() {
        return withPartial;
    }

    public void setWithPartial(Ref<Other> withPartial) {
        this.withPartial = withPartial;
    }

    public Ref<Other> getWithEveryhthing() {
        return withEveryhthing;
    }

    public void setWithEveryhthing(Ref<Other> withEveryhthing) {
        this.withEveryhthing = withEveryhthing;
    }

    public Ref<Other> getUnlessStopper() {
        return unlessStopper;
    }

    public void setUnlessStopper(Ref<Other> unlessStopper) {
        this.unlessStopper = unlessStopper;
    }
}

Then I wrote the following code.

Other other = new Other();

Key<Other> otherKey = ofy().save().entity(other).now();

Thing thing = new Thing();
thing.setWithPartial(Ref.create(otherKey));

Key<Thing> thingKey = ofy().save().entity(thing).now();

Thing t = ofy().load().key(thingKey).now();

System.out.println("Is loaded: " + t.getWithPartial().isLoaded());

Without writing ofy().load().group(Partial.class).key(thingKey).now(); other entity still loads into session. However in documentation it needs group class to be loaded.

1

1 Answers

0
votes

The isLoaded() method tests whether the entity is in the session cache. Since you just save()d the entity, it's already in the session cache.

If you want to test the behavior of load groups, you need to ofy().clear() the cache.