我需要一些关于使用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)*
因此,来自同一实体表的外键似乎有问题。 还有其他办法吗?
答案 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()
categoryC
和categoryD
的列表