简体   繁体   中英

Defining inner classes in AppEngine Datastore (Objectify)

In AppEngine I need to have an entity Diagram that contains an id , title and a variable list of elements of inner class Box , each one with id and description .

Please find below the definition. However, at time of defining the EntityProxy List getter and 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);
[...]

Your inner class must be static . Nonstatic inner classes have an implicit link to an instance of the outer class, which would be really confusing from the perspective of loading and saving entities to the datastore.

Confusing, you have a Collection<Box> in the Box class? Doesnt sound right.. Anyways the inner Box class must be market static or be moved to a different file. Use the @Embed (version 4.0) annotation on the Box class.

Also, assuming DatastoreObject is the base of all your entities, you can make DatastoreObject as an @Entity and all its sub classes as an @EntitySubClass (index = true) . Obviously all sub entities would be be saved under the same 'kind' (DatastoreObject) in the datastore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM