Greendao树实体实现:来自同一实体的父和子,不编译

时间:2016-04-07 22:11:02

标签: tree entity greendao

我需要一些关于使用ORM GREENDAO定义的实体进行树表示的建议。

事实上,我无法使用自反属性生成dao文件。 我试图实现一个简单的CATEGORIES树。

类别1可以将类别0作为父类,类别1也可以将多个类别作为子类。因此,两个外键指向与源和目标相同的实体类别。

我尝试了greendao在其网站上提出的建立树实体的代码,请参阅Section "Modelling Tree Relations (Example)" on the page

我有问题的代码:

Entity categories = schema.addEntity("CATEGORIES");
categories.addIdProperty().primaryKey();
Property parentId= categories.addLongProperty("parentId").notNull().getProperty();
ToOne ParentCat = categories.addToOne(categories, parentId);
ParentCat.setName("parentId");
Property childrenId= categories.addLongProperty("childrenId").notNull().getProperty();
ToMany ChildrenCat = categories.addToMany(categories, childrenId);
ChildrenCat.setName("children");

我从编译器中得到了这些例外:

greenDAO Generator
Copyright 2011-2015 Markus Junginger, greenrobot.de. Licensed under GPL V3.
This program comes with ABSOLUTELY NO WARRANTY
*Exception in thread "main" java.lang.RuntimeException: Currently only single FK columns are supported: ToOne 'parentId' from CATEGORIES to CATEGORIES
    at de.greenrobot.daogenerator.ToOne.init3ndPass(ToOne.java:91)
    at de.greenrobot.daogenerator.Entity.init3rdPassRelations(Entity.java:603)
    at de.greenrobot.daogenerator.Entity.init3rdPass(Entity.java:596)
    at de.greenrobot.daogenerator.Schema.init3rdPass(Schema.java:185)
    at de.greenrobot.daogenerator.DaoGenerator.generateAll(DaoGenerator.java:91)
    at de.greenrobot.daogenerator.DaoGenerator.generateAll(DaoGenerator.java:79)
    at Apptree_DAO_Generator.main(Apptree_DAO_Generator.java:13)*

*Exception in thread "main" java.lang.RuntimeException: Source properties do not match target properties: ToMany 'children' from CATEGORIES to CATEGORIES
    at de.greenrobot.daogenerator.ToMany.init2ndPass(ToMany.java:59)
    at de.greenrobot.daogenerator.Entity.init2ndPass(Entity.java:517)
    at de.greenrobot.daogenerator.Schema.init2ndPass(Schema.java:179)
    at de.greenrobot.daogenerator.DaoGenerator.generateAll(DaoGenerator.java:90)
    at de.greenrobot.daogenerator.DaoGenerator.generateAll(DaoGenerator.java:79)
    at Apptree_DAO_Generator.main(Apptree_DAO_Generator.java:13)*

因此,来自同一实体表的外键似乎有问题。 还有其他办法吗?

1 个答案:

答案 0 :(得分:0)

实际上,您需要单个双向1对n关系。尝试这样的事情:

<details id="foo">Details <summary>Summary</summary></details>
<div><a href="#foo">#foo</a> <a href="#bar">#bar</a></div>

之后,您必须为每个类别设置父级

Entity categories = schema.addEntity("CATEGORIES");
categories.addIdProperty().primaryKey();
Property parentId= categories.addLongProperty("parentId").notNull().getProperty();
ToOne ParentCat = categories.addToOne(categories, parentId);
ParentCat.setName("parent");
ToMany ChildrenCat = categories.addToMany(categories, parentId);
ChildrenCat.setName("children");

当您需要使用关系时,使用categoryB.setParent(categoryA); categoryC.setParent(categoryB); categoryD.setParent(categoryB); ,您将通过调用categoryB获得categoryA。对于孩子们来说也是如此,当您拨打categoryB.getParent()

时,您会看到包含categoryCcategoryD的列表