在 GAE/J JDO 中持久存储内部类的正确方法是什么?

发布于 2024-11-04 18:00:07 字数 925 浏览 1 评论 0原文

GAE 文档中,我可以读到以下内容:

实体的种类源自类的简单名称(内部类使用不带包名称的 $ 路径)。

它没有告诉我如何存储它,并且:

这是一个嵌入类的示例。此示例使嵌入类成为使用它的数据类的内部类;这很有用,但不是使类可嵌入所必需的。

为什么它有用?让类“嵌入”有什么好处?如果我不需要“嵌入”它,我该如何进行? 我很难理解最后这句话。

假设我有这样的课程:

@PersistenceCapable
public class ChatHistory {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private ArrayList<Message> messages;
    // more fields and all the ChatHistory methods here

    private class Message implements Comparable<Message>{ // <-- This one
        public String timeStamp;
        public String text;
        // more fields and all the Message methods here
    }
}

你将如何存储它?

From GAE's documentation, I can read this:

The entity's kind is derived from the simple name of the class (inner classes use the $ path without the package name).

which does not tell me how to store it, and this:

Here is an example of an embedded class. This example makes the embedded class an inner class of the data class that uses it; this is useful, but not required to make a class embeddable.

Why is it useful? What is the advantage of making the class "embedded"? How would I proceed if I do not need to "embed" it? I'm having a hard time understanding this last statement.

Let's say I have this class:

@PersistenceCapable
public class ChatHistory {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private ArrayList<Message> messages;
    // more fields and all the ChatHistory methods here

    private class Message implements Comparable<Message>{ // <-- This one
        public String timeStamp;
        public String text;
        // more fields and all the Message methods here
    }
}

How would you store it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高速公鹿 2024-11-11 18:00:07

最明显的事情是将 Message 放入静态类(尝试持久化非静态类的想法让我头疼)并用 @PersistenceCapable 注释它。这将导致它变成自己的表。最好建议您将其移动到自己的文件中(并将其更改为“package private”)。

The most obvious thing would be to make Message into a static class (the idea of trying to persist non-static classes makes my head hurt) and to annotate it with @PersistenceCapable. That will then cause it to be turned into its own table. You might be best advised to move it into its own file (and change it to 'package private') instead.

聊慰 2024-11-11 18:00:07

Objectify - 基于 GAE 构建的便捷数据存储界面 - 定义 @Embedded适合这种情况。如果您还没有寻找这种工具,这似乎是一个很好的机会。如果您这样做并且更喜欢低级 API,我无法为您提供更多帮助。

Objectify — a convenient datastore interface builded on top of GAE — defines @Embedded for this kind of situation. If you don't have look for this kind of tool yet, it seems to be a good opportunity. If you do and prefere the low-level API, I cannot help you any further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文