使用 GAE Datastore 和 Objectify 在 ArrayList 中存储和检索项目

Store and Retrieve items in an ArrayList using GAE Datastore and Objectify

我正在尝试编写一个查询来检索数据存储实体的 ArrayList,以便我可以将其显示在 JSP 上。我能够成功地将实体添加到数据存储区,并且我可以在开发控制台中看到 ArrayList 也在那里。

我需要嵌入 ArrayList 才能实现吗?嵌入后检索列表的查询是什么?

我正在添加到数据存储区的 SharedCorpse class。

@Entity
public class SharedCorpse extends Corpse {

    @Id Long id;
    @Index long corpseID;


    public SharedCorpse(ArrayList<CorpseLyric> corpseLyrics, long corpseID) {
        super(corpseLyrics);
        this.corpseID = corpseID;
    }

}

Corpse class SharedCorpse 扩展(以防有帮助)

public class Corpse implements Serializable {

    @Index private ArrayList<CorpseLyric> corpseLyrics;

    public Corpse() {
        corpseLyrics = new ArrayList<CorpseLyric>();
    }

    public Corpse(ArrayList<CorpseLyric> corpseLyrics) {
        this.corpseLyrics = corpseLyrics;
    }

    public void addLyricSnippet(CorpseLyric corpseLyric) {
        corpseLyrics.add(corpseLyric);
    }

    public void removeLyricSnippet(CorpseLyric corpseLyric) {
        corpseLyrics.remove(corpseLyric);
    }

    public ArrayList<CorpseLyric> getCorpseLyrics() {
        return corpseLyrics;
    }
}

我当前的查询:

SharedCorpse sharedCorpse = ObjectifyService.ofy().load().type(SharedCorpse.class).filter("corpseID",sharedID).first().now();

我用来显示 ArrayList 中项目的 JSP 代码:

<table>
    <c:forEach var="corpseLyric" items="${sharedCorpse.corpseLyrics}">
        <tr>
            <td>${corpseLyric.snippet}</td>
        </tr>
    </c:forEach>
</table>

您不需要做任何特别的事情。如果你的尸体有一个列表类型的字段,它将被完整地存储和检索。您不需要(并且可能不应该)@Index 字段。

此外,您可能不想维护单独的 idcorpseID 字段。只需使用 id 并按键加载实体(或使用 type().id() 快捷方式)。

您可能想阅读此内容:https://github.com/objectify/objectify/wiki/Entities