查询 Android 房间中的 1:many 关系

Query 1:many relation in Android Room

我在我的 Android 应用程序 (Java) 中使用 Room,我有两个具有 1:many 关系的实体。

镜头实体 一个镜片可以多次佩戴。

@Entity(tableName = "lens_table")
public class Lens {

    @PrimaryKey(autoGenerate = true)
    private int lensId;
    private String name;
}

穿实体 一次佩戴只能对应一个镜片。

@Entity(tableName = "wear_table",
        foreignKeys = {@ForeignKey(
                entity = Lens.class,
                parentColumns = "lensId",
                childColumns = "fk_lensId",
                onDelete = ForeignKey.CASCADE)},
        indices = {@Index("fk_lensId")})
public class Wear {

    @PrimaryKey(autoGenerate = true)
    private int wearId;
    private String name;
    private int fk_lensId;
}

到目前为止一切顺利。到目前为止,我对“标准”查询(创建、获取所有、更新、删除......)没问题,那里有很多文档。我也成功地实施了查询,以根据以下关系获取所有镜片的磨损情况。

public class LensWithWears {

    @Embedded
    public Lens lens;

    @Relation(
            parentColumn = "lensId",
            entityColumn = "fk_lensId"
    )
    public List<Wear> wears;
}

但是我现在需要查询以下信息:

Get a single wear with the associated lens by looking up the wearId

我目前使用的关系 class 如下所示:

public class WearWithLens {
    @Embedded
    public Wear wear;

    @Relation(
            parentColumn = "wearId",
            entityColumn = "lensId"
    )
    public Lens lens;
}

Dao 查询看起来像这样:

@Query("SELECT * FROM wear_table WHERE wearId = :wearId LIMIT 1")
LiveData<WearWithLens> getWearWithLensByWearId(int wearId);

我的代码显然不行,不然我也不会问... 问题是,返回了一个 WearWithLens 对象,但其中的镜头对象始终为 null。

换句话说,我想查询一个与镜头有 1:1 关系的 Wear,并在 class WearWithLens 中将两个对象放在一起。

有人可以告诉我查询应该是什么样子吗?

谢谢!

父列需要是构成两者关系的列。也就是说它应该是 fk_lensId 列。

所以 :-

public class WearWithLens {
    @Embedded
    public Wear wear;

    @Relation(
            parentColumn = "fk_lensId",
            entityColumn = "lensId"
    )
    public Lens lens;
}

举个例子

  • 不使用 LiveData<WearWithLens> getWearWithLensByWearId(int wearId);,而是使用 WearWithLens getWearWithLensByWearId(int wearId); 以方便和简洁。
  • 使用您的 classes/entities 添加了 getter 和 setter 以及额外的构造函数来减少编码
  • 显然是上面的@Relationship。

具有以下内容:-

    db = TheDatabase.getInstance(this);
    dao = db.getAllDao();

    int l1id = (int) dao.insert(new Lens("Lens1"));
    int l2id = (int) dao.insert(new Lens("Lens2"));
    int l3id = (int) dao.insert(new Lens("Lens3"));

    dao.insert(new Wear("Wear1 child of Lens1",l1id));
    dao.insert(new Wear("Wear2 child of Lens1",l1id));
    dao.insert(new Wear("Wear3 child of Lens1",l1id));
    dao.insert(new Wear("Wear4 child of Lens2",l2id));
    dao.insert(new Wear("Wear5 child of Lens2",l2id));
    dao.insert(new Wear("Wear6 child of Lens2",l2id));
    dao.insert(new Wear("Wear7 child of Lens3",l3id));

    for (Wear wear: dao.getAllWears()) {
        WearWithLens currentWearWithLens = dao.getWearWithLensByWearId(wear.getWearId());
        Log.d("DBINFO","Current Wear is " + currentWearWithLens.wear.getName() + " parent Lens is " + currentWearWithLens.lens.getName());
    }

结果是:-

2021-12-09 06:34:58.105 D/DBINFO: Current Wear is Wear1 child of Lens1 parent Lens is Lens1
2021-12-09 06:34:58.110 D/DBINFO: Current Wear is Wear2 child of Lens1 parent Lens is Lens1
2021-12-09 06:34:58.112 D/DBINFO: Current Wear is Wear3 child of Lens1 parent Lens is Lens1
2021-12-09 06:34:58.114 D/DBINFO: Current Wear is Wear4 child of Lens2 parent Lens is Lens2
2021-12-09 06:34:58.115 D/DBINFO: Current Wear is Wear5 child of Lens2 parent Lens is Lens2
2021-12-09 06:34:58.116 D/DBINFO: Current Wear is Wear6 child of Lens2 parent Lens is Lens2
2021-12-09 06:34:58.120 D/DBINFO: Current Wear is Wear7 child of Lens3 parent Lens is Lens3