【问题标题】:Defining inner classes in AppEngine Datastore (Objectify)在 AppEngine Datastore (Objectify) 中定义内部类
【发布时间】:2013-11-06 04:58:12
【问题描述】:

在 AppEngine 中,我需要一个 entity Diagram,其中包含一个 idtitle 和一个可变的元素列表 inner class Box,每个元素都有 iddescription

请在下面找到定义。但是,在定义 EntityProxy List getter 和 setter 时:"The type java.util.List<Box> cannot be used here"

DIAGRAM.java

@Entity
public class Diagram extends DatastoreObject {

    public class Box {
    private String boxId;
    private String description;
    public String get_id() {
        return boxId;
    }
    public void set_id(String boxId) {
        this.boxId = boxId;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    @Indexed private String diagramId; // Primary key
    @Indexed private String title;
    @Embedded private List<Box> boxes;

    public String get_id() {
        return diagramId;
    }
    public void set_id(String diagramId) {
        this.diagramId = diagramId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public void setBoxes(List<Box> boxes) {
        this.boxes = boxes
    }
    public List<Box> getBoxes() {
        return boxes;
    }
}

DIAGRAMPROXY.java

[...]
    List<Box> getBoxes();
    void setBoxes(List<Box> boxes);
[...]

【问题讨论】:

  • 那不是子类,那是内部类。
  • 好点!但问题是一样的。有什么想法吗?
  • 也许把它从课堂上拿出来,让它成为一个单独的课堂,看看是否有帮助?
  • 已经试过了。同样的错误...
  • 也许您需要将其声明为嵌入式类?

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


【解决方案1】:

你的内部类必须是static。非静态内部类有一个到外部类实例的隐式链接,从加载和保存实体到数据存储区的角度来看,这会让人很困惑。

【讨论】:

    【解决方案2】:

    令人困惑,您在 Box 类中有 Collection&lt;Box&gt; 吗?听起来不对。无论如何,内部 Box 类必须是市场 static 或移动到不同的文件。在 Box 类上使用@Embed(4.0 版)注解。

    另外,假设DatastoreObject 是所有实体的基础,您可以将DatastoreObject 设为@Entity,并将其所有子类设为@EntitySubClass (index = true)。显然,所有子实体都将保存在数据存储区中相同的“种类”(DatastoreObject)下。

    【讨论】:

      猜你喜欢
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 2018-11-05
      • 1970-01-01
      相关资源
      最近更新 更多