简体   繁体   English

Android Room Pojo对象的功能不一致

[英]Android Room Pojo Object inconsist functionality

During development I face situation where I have to save Object with fields and List 在开发过程中,我遇到了必须将对象与字段和列表一起保存的情况

Looking for the solution I came across Room @Relation 寻找解决方案时,我遇到了Room @Relation

I've damm UserAndPets example 我有UserAndPets示例

class ShoppingListAndItems(
    @Embedded
    var shoppingList: ShoppingListCache = ShoppingListCache.emptyInstance(),
    @Relation(
        parentColumn = ShoppingListCache.COLUMN_PARENT_ID,
        entityColumn = ShoppingItemCache.ENTITY_COLUMN)
    var shoppingItems: List<ShoppingItemCache> = emptyList()
)

The problem with that kind solution, using following DAO - list is not being updated: 使用以下DAO的那种解决方案的问题-列表未更新:

@Query("""SELECT * FROM ${ShoppingListCache.TABLE_NAME}
    WHERE ${ShoppingListCache.COLUMN_IS_ARCHIVED} = :isArchived""")
fun getListOfShoppingList(isArchived: Boolean): List<ShoppingListAndItems>

Moreover that POJO cannot be use more then return type - what is disappointing 此外,POJO不能再使用然后返回类型-令人失望的是

@Insert
fun insertShoppingListAndItems(newShoppingListAndItems: ShoppingListAndItems) // Error

@Delete
fun insertShoppingListAndItems(newShoppingListAndItems: ShoppingListAndItems) // Error

Am I doing something wrong or Room doesn't support such functionality? 我是在做错什么,还是Room不支持这种功能?

You cannot use a non-entity with @Insert , @Update , @Delete . 你不能使用非实体@Insert@Update@Delete Honestly, there is no native solution to use non-entity class with any of the above annotation, but there are few workarounds. 老实说,没有本机解决方案可将非实体类与上述任何注释一起使用,但解决方法很少。

Take a look at this already answered question showcasing various workarounds to do: Android Room: Insert relation entities using Room 看看这个已经回答的问题,展示解决方案: Android Room:使用Room插入关系实体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM