use*_*561 7 android android-room android-architecture-components
我之间的实体之间的关系应如下所示:
概述->有很多->类别->有很多->交易
如果Category.overviewId == Overview.id,则类别应包括在Overview中。我很确定我应该能够使用@Relation注解来实现此目标,例如:
@Relation(parentColumn = 'id', entityColumn = 'overviewId')
List<Category> categories;
Run Code Online (Sandbox Code Playgroud)
然后,我要在Category实体内包括Transaction.overviewId == Category.overviewId && Transaction.categoryId == Category.categoryId的所有事务。这是我正在努力的部分,因为@Relation批注似乎只能考虑1列。我想我想要类似的东西:
@Relation(parentColumn = {'overviewId','categoryId'}, entityColumn = {'overviewId','categoryId'})
List<Transaction> transactions;
Run Code Online (Sandbox Code Playgroud)
但这似乎不是一种有效的处理方式。有谁知道我应该如何做到这一点?我想我最终希望能够观察到一个查询,该查询将在相关的概述,类别或事务更新时更新,因此将类别和事务嵌入到概述中似乎是最好的方法,但是如果有更好的选择听到它真是太好了。
遇到同样的问题,想出了@Transaction
@Query("SELECT * FROM store_table WHERE userId = :userId AND storeId = :storeId")
fun getStore(storeId:String, userId:String): StoreDb?
@Query("SELECT * FROM product_table WHERE userId = :userId AND storeId = :storeId")
fun getProducts(storeId:String, userId:String):List<ProdusctsDb>
@Transaction
fun getStoreWithProducts(storeId:String, userId:String): StoreWithProductsDb{
val storeDb: StoreDb = getStore(storeId,userId) ?: return null
val products = getProducts(storeId,userId)
return StoreWithProductsDb(storeDb, products)
}
Run Code Online (Sandbox Code Playgroud)
其中 StoreWithProductsDb 是常用数据类
data class StoreWithProductsDb(val storeDb: StoreDb, val products: List<ProductsDb>)
Run Code Online (Sandbox Code Playgroud)
PS:刚刚采用的示例所有名称都是随机的
| 归档时间: |
|
| 查看次数: |
282 次 |
| 最近记录: |