实体与实体之间的关系
Note: This article is part of the advanced Room series which covers all the details about the Room persistence library. You can read all the articles here:
注意:本文是高级Room系列的一部分,该系列涵盖有关Room持久性库的所有详细信息。 您可以在此处阅读所有文章:
Entity Relationship in Room [You are here]
房间中的实体关系[您在这里]
房间中的实体关系 (Entity Relationships in Room)
This article covers how we can define relationship between entities in Room persistence library.
本文介绍了如何在Room Persistence库中定义实体之间的关系。
Since SQLite is a relational database, entities can have relationships between them. In Room, entities cannot directly reference other entities because it might cause loading unnecessary data every time.
由于SQLite是一个关系数据库,因此实体之间可以具有关系。 在Room中,实体无法直接引用其他实体,因为这可能会导致每次加载不必要的数据。
Still sometimes, you would want to reference other entities from your entity. You can acheive this in multiple ways.
有时,您仍想从您的实体中引用其他实体。 您可以通过多种方式来实现。
嵌入式对象 (Embedded Objects)
You can use the @Embedded
annotation to represent an object that you'd like to decompose into its subfields within a table(entity). You can then query the embedded fields just as you would for other individual columns.
您可以使用@Embedded
批注表示要分解为表(实体)内其子字段的对象。 然后,您可以查询嵌入字段,就像查询其他各个