如何使用 greenDAO 将一个对象加载到另一个对象中?

How to load an object inside another object using greenDAO?

我有这些实体:

public class Category {

    private Long id;
    private String name;
    private Integer color;
}

public class Item {

    private Long id;
    private String name;
    private String groupType;
    private Category category;
}

我应该如何为 greenDAO 建模以在 Item 对象中包含类别?

编辑:

生成器代码:

public class GreenDaoGenerator {

    public static final String DEFAULT_PACKAGE = "xxxxx";

    public static void main(String[] args) throws Exception {
        Schema schema = new Schema(1, DEFAULT_PACKAGE + ".entity");
        schema.setDefaultJavaPackageDao(DEFAULT_PACKAGE + ".dao");

        addCategory(schema);
        addItem(schema);

        DaoGenerator daoGenerator = new DaoGenerator();
        daoGenerator.generateAll(schema, "Generated Files");
    }

    private static void addItem(Schema schema) {
        Entity entity = schema.addEntity("Item");
        entity.addIdProperty();
        entity.addStringProperty("Name");
        entity.addStringProperty("GroupType");
        Property IdProperty = entity.addLongProperty("categoryId").getProperty();
        entity.addToOne(schema.getEntities().get(0), IdProperty);
    }

    private static void addCategory(Schema schema) {
        Entity entity = schema.addEntity("Category");
        entity.addIdProperty();
        entity.addStringProperty("Name");
        entity.addIntProperty("Color");
    }

}

我在你的回答后做了,请看看我做的是否正确。谢谢

您必须使用 GreenDao Relations,将 Item 实体映射到 Category 实体。

这是一个示例(其中 itemcategory 是您的 greenDAO 生成器代码中的实体):

Property IdProperty = item.addLongProperty("categoryId").getProperty();
item.addToOne(category, IdProperty);