使用 @Load 对象化 Ref<> 不适用于 ofy().load().group()

Objectify Ref<> with @Load is not working with ofy().load().group()

我在 @Load 中使用 Ref<> 时遇到问题。基本上我从 Objectify 网站复制粘贴来测试带有负载组的 @Load 注释。

@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;
    }
}

然后我写了下面的代码

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());

不写y().load().group(Partial.class).key(thingKey).now();其他实体仍然加载到会话中。但是在文档中它需要加载组 class。

isLoaded()方法测试实体是否在会话缓存中。由于您只是 save()d 实体,它已经在会话缓存中。

如果要测试加载组的行为,需要ofy().clear()缓存。