0

During development I face situation where I have to save Object with fields and List

Looking for the solution I came across Room @Relation

I've damm UserAndPets example

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:

@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

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

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

Am I doing something wrong or Room doesn't support such functionality?

murt
  • 3,790
  • 4
  • 37
  • 48
  • Possible duplicate of [Android Room: Insert relation entities using Room](https://stackoverflow.com/questions/44667160/android-room-insert-relation-entities-using-room) – Akshay Chordiya Mar 26 '18 at 05:41

1 Answers1

0

You cannot use a non-entity with @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

Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52