user411103
user411103

Reputation:

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);
[...]

Upvotes: 0

Views: 430

Answers (2)

Shaun
Shaun

Reputation: 199

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.

Upvotes: 0

stickfigure
stickfigure

Reputation: 13556

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.

Upvotes: 2

Related Questions