Entity list relationships in Android Room

Viewed 9289

I'm trying to load entity sublist, but i would like to avoid to do 2 queries.

I'm thinking about do query inside TypeConverter, but i really don't know if it's good idea.

My entities:

@Entity
class Region(
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0,
        var name: String = "",
        var locales: List<Locale> = listOf())

@Entity(foreignKeys = arrayOf(ForeignKey(
        entity = Region::class,
        parentColumns = arrayOf("id"),
        childColumns = arrayOf("regionId"),
        onDelete = CASCADE,
        onUpdate = CASCADE
)))
class Locale(
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0,
        var regionId: Int = 0,
        var name: String = "")

DAOs :

@Dao
interface RoomRegionDao{
    @Insert
    fun insert(region: Region)

    @Delete
    fun delete(region: Region)

    @Query("select * from region")
    fun selectAll(): Flowable<List<Region>>
}

@Dao
interface RoomLocaleDao{
    @Insert
    fun insert(locale: Locale)

    @Query("select * from locale where regionId = :arg0")
    fun selectAll(regionId: Int): List<Locale>
}

Database:

@Database(entities = arrayOf(Region::class, Locale::class), version = 1)
@TypeConverters(RoomAppDatabase.Converters::class)
abstract class RoomAppDatabase : RoomDatabase() {
    abstract fun regionDao(): RoomRegionDao
    abstract fun localeDao(): RoomLocaleDao

    inner class Converters {
        @TypeConverter
        fun toLocales(regionId: Int): List<Locale> {
            return localeDao().selectAll(regionId)
        }

        @TypeConverter
        fun fromLocales(locales: List<Locale>?): Int {
            locales ?: return 0
            if (locales.isEmpty()) return 0

            return locales.first().regionId
        }
    }
}

It's not working because can't use inner class as converter class.

  • Is it a good way?
  • How can I load "locales list" automatically in region entity when I do RoomRegionDao.selectAll?
3 Answers

You can get all your Regions wit a list of Locales inside each Region just with one Query.

All you need to do is create a new Relation like this:

data class RegionWithLocale(
    @Embedded
    val region: Region,
    @Relation(
        parentColumn = "id",
        entity = Locale::class,
        entityColumn = "regionId"
    )
    val locales: List<Locale>
)

And latter get that relation in your Dao like this:

@Query("SELECT * FROM region WHERE id IS :regionId")
fun getRegions(regionId: Int): LiveData<RegionWithLocale>

@Query("SELECT * FROM region")
fun getAllRegions(): LiveData<List<RegionWithLocale>>

This solution worked for me

ListConvert.kt

class ListConverter
{
    companion object
    {
        var gson = Gson()

        @TypeConverter
        @JvmStatic
        fun fromTimestamp(data: String?): List<Weather>?
        {
            if (data == null)
            {
                return Collections.emptyList()
            }

            val listType = object : TypeToken<List<String>>() {}.type
            return gson.fromJson(data, listType)
        }

        @TypeConverter
        @JvmStatic
        fun someObjectListToString(someObjects: List<Weather>?): String?
        {
            return gson.toJson(someObjects)
        }
    }
}

The property inside of my Entity

@SerializedName("weather")
@TypeConverters(ListConverter::class)
val weather: List<Weather>,
Related