使用Realm的Nosql数据库结构

时间:2017-12-02 14:39:00

标签: sqlite nosql realm data-modeling

我有一个SQLite数据库要迁移到Realm数据库。

其中包括assoc表,plant表和definition表:

enter image description here

关联表关联两个工厂。定义表以不同的语言表达植物的名称。

由于我使用映射到我的POJO对象的Realm数据库,因此我将一个类映射到我的assoc表(相当于Realm)。此assoc类包含plant_id1和plant_id2的两个列表。问题是我必须能够检索所有植物的所有关联,但我必须能够 找到所有不同的植物,而不需要关联....我想有一些我不理解使用nosql的数据建模...但我怎么能这样做?我是否必须在assoc中以及在assoc之外的植物列表中复制我的植物数据?我不确定在这里完全清楚......

1 个答案:

答案 0 :(得分:1)

  

我想我使用nosql进行数据建模时无法理解......但我该怎么办呢?

public class Plant extends RealmObject {
    // PK
    RealmList<Plant> associatedPlants;

    @LinkingObjects("plant1")
    private final RealmResults<Assoc> assoc1 = null;

    @LinkingObjects("plant2")
    private final RealmResults<Assoc> assoc2 = null;

    @LinkingObjects("associatedPlants")
    private final RealmResults<Plant> associatedByPlants = null;
}

public class Assoc extends RealmObject {
    // PK
    Plant plant1;
    Plant plant2;
}

您的定义似乎是错误的,如果您想要多个定义属于工厂,那么您不能在工厂中拥有definitionId。不管怎样,我很可能只为每个语言名称都有一个字段。