Objectify List Ref fetch

时间:2016-06-23 00:50:45

标签: java google-app-engine objectify google-cloud-datastore

我有以下课程:

Message.class

@Entity(name = "MSG")
public class Message {

    public static class LoadTag {
    };

    @Id
    @Index
    private Long id;
    @Index
    private String username;
    @Index
    private Date createdOn;
    @Load(LoadTag.class)
    @Index
    private List<Ref<Tag>> tagsRefs = new ArrayList<>();
    @Ignore
    private List<Tag> tags;

    public void addTag(Tag tag) {
    Ref<Tag> ref = Ref.create(tag.getKey());
    if (!tagsRefs.contains(ref)) {
        tagsRefs.add(ref);
    }
    }

    @OnLoad
    public void deRef() {
    if (tagsRefs != null) {
        tags = new ArrayList<>();
        for (Ref<Tag> tagLoaded : tagsRefs) {
        if (tagLoaded.isLoaded()) {
            tags.add(tagLoaded.get());
        }
        }
    }
    }

    public Quote() {
    }

    public Long getId() {
    return id;
    }

    public void setId(Long id) {
    this.id = id;
    }

    public void setTags(List<Ref<Tag>> tags) {
    this.tagsRefs = tags;
    }

    public List<Tag> getTags() {
    return this.tags;
    }

    public Date getCreatedOn() {
    return createdOn;
    }

    public void setCreatedOn(Date createdOn) {
    this.createdOn = createdOn;
    }

    public String getUsername() {
    return username;
    }

    public void setUsername(String username) {
    this.username = username;
    }

}

Tag.class

@Entity(name = "TAG")
public class Tag {

    @Id
    private Long id;
    @Index
    private String tag;

    public Key<Tag> getKey() {
    return Key.create(Tag.class, getId());
    }

    public Tag(String tag) {
    this.tag = tag;
    }

    public String getTag() {
    return tag;
    }

    public void setTag(String tag) {
    this.tag = tag;
    }

    public Long getId() {
    return id;
    }

    public void setId(Long id) {
    this.id = id;
    }

}

这就是我试图在我的服务中加载所有带有标签的消息:

public List<Message> getWithTags(String username) {
return ObjectifyService.ofy().load().group(Message.LoadTag.class).type(Message.class).filter("username", username).order("-createdOn").list();
}

我正在使用@OnLoad填充我的tags,但在我调试时,我可以看到tagsRefs始终为空。有什么想法吗?

0 个答案:

没有答案
相关问题