GreenDao多对多关系 [英] GreenDao Many to Many Relation
问题描述
我用Greendao在我的Android数据库映射。到目前为止,一切都很正常。
I use Greendao for my database mapping in Android. So far everything works great.
但现在我有以下问题:
我有一个群集和叶对象。一个集群包含许多叶对象。
如果群集被放大它可以在更多的集群和多叶分手了。这并不是一个严格的树关系更是一个大簇具有大量的叶子,但一小簇可具有这样叶子的子集。
I have a Cluster and leaf Objects. A cluster contains many leaf objects. If a cluster is zoomed it can split up in more clusters and more leafs. This is not a strict tree relationship it is more that a big cluster has a lots of leafs but a small cluster can have a subset of this leafs.
基本上是一个群集有很多叶子,但叶子也都在多个群集,因为叶子的ID是唯一的,关系到集群保存在叶叶只能在一个集群吧?
Basically a cluster has many leafs but leafs also are in multiple clusters, since the id of a leaf is unique and the relation to the cluster is saved in the leaf a leaf can only be in one cluster right?
该greendao主页指出,多对多的关系是可能的,但我怎么也无法让我的头围绕它如何将这种关系模型。
The greendao homepage states that many to many relations are possible but how i can't get my head around it how to model this relation.
是否可以模拟这种关系或者是数据结构GreenDao问题?
Is it possible to model such a relation or is the data structure a problem for GreenDao?
推荐答案
让我们开始从报价文档:
虽然greenDAO不支持N:直接米的关系,截至目前,你
可以模拟连接表作为一个独立的实体。在实践中,你经常
有关系实体有额外的属性,所以你可能要
这样做的反正。在未来的版本中,greenDAO可能直接引入
支持的n:M关系
While greenDAO does not support n:m relations directly as of now, you can model the join table as a separate entity. In practice, you often have "relation entities" with additional properties, so you might want to do so anyway. In future releases, greenDAO might introduce direct support of n:m relations.
所以,你的情况,你必须一个实体模型,即集群与叶之间。让我们把这个实体ClusterLeaf。为了有效地查询叶子为群集,我建议原查询的,因为QueryBuilder的不支持加入呢。
So, in your case you have to model an entity, that is between Cluster and Leaf. Let's call this entity ClusterLeaf. To query leafs for a cluster efficiently, I'd suggest raw queries because the QueryBuilder does not support joins yet.
这篇关于GreenDao多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!