Error using TypeConverter (GSON) for Room with custom objects in Kotlin

I am receiving the error
Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
for “settings”.

I don’t know what I’m doing wrong, since I have a conversor for Settings.
In Converters, “fromScore”, “toScore”, “fromSettings” and “toSettings” are being used. The rest of the methods are greyed out.

Also feel free to suggest other (possibly better) ways than Room to store this data, please. Thanks.

The entity:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Entity(tableName = "user_table")
data class User(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id") val id: Int = 0,
@ColumnInfo(name = "score") val score: Score = Score(),
@ColumnInfo(name = "settings") val settings: List<Settings> = emptyList(),
) {
data class Score(
val score: Int = 0,
val correctAnswers: Int = 0,
)
data class Settings(
val name: String = "",
val mode: Set<GameMode> = emptySet(),
val selectionMode: Boolean = false,
val questionLimit: QuestionLimit = QuestionLimit.FIFTEEN,
val continents: List<Continent> = emptyList(),
) {
enum class GameMode {
COUNTRY_CAPITAL, CAPITAL_COUNTRY
}
enum class QuestionLimit(val number: String) {
FIFTEEN("15"),
THIRTY("30"),
FIFTY("50"),
MAX("Max")
}
}
}
</code>
<code>@Entity(tableName = "user_table") data class User( @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Int = 0, @ColumnInfo(name = "score") val score: Score = Score(), @ColumnInfo(name = "settings") val settings: List<Settings> = emptyList(), ) { data class Score( val score: Int = 0, val correctAnswers: Int = 0, ) data class Settings( val name: String = "", val mode: Set<GameMode> = emptySet(), val selectionMode: Boolean = false, val questionLimit: QuestionLimit = QuestionLimit.FIFTEEN, val continents: List<Continent> = emptyList(), ) { enum class GameMode { COUNTRY_CAPITAL, CAPITAL_COUNTRY } enum class QuestionLimit(val number: String) { FIFTEEN("15"), THIRTY("30"), FIFTY("50"), MAX("Max") } } } </code>
@Entity(tableName = "user_table")
data class User(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id") val id: Int = 0,
    @ColumnInfo(name = "score") val score: Score = Score(),
    @ColumnInfo(name = "settings") val settings: List<Settings> = emptyList(),
) {

    data class Score(
        val score: Int = 0,
        val correctAnswers: Int = 0,
    )

    data class Settings(
        val name: String = "",
        val mode: Set<GameMode> = emptySet(),
        val selectionMode: Boolean = false,
        val questionLimit: QuestionLimit = QuestionLimit.FIFTEEN,
        val continents: List<Continent> = emptyList(),
    ) {
        enum class GameMode {
            COUNTRY_CAPITAL, CAPITAL_COUNTRY
        }

        enum class QuestionLimit(val number: String) {
            FIFTEEN("15"),
            THIRTY("30"),
            FIFTY("50"),
            MAX("Max")
        }
    }
}

The converters:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Converters {
private val gson = Gson()
// SCORE
@TypeConverter
fun fromScore(value: User.Score): String {
return gson.toJson(value)
}
@TypeConverter
fun toScore(value: String): User.Score {
return gson.fromJson(value, User.Score::class.java)
}
// SETTINGS
@TypeConverter
fun fromSettings(value: List<User.Settings>): String {
return gson.toJson(value)
}
@TypeConverter
fun toSettings(value: String): List<User.Settings> {
val type = object : TypeToken<List<User.Settings>>() {}.type
return gson.fromJson(value, type)
}
// QUESTION LIMIT
@TypeConverter
fun fromQuestionLimit(value: QuestionLimit): String {
val type = object : TypeToken<QuestionLimit>() {}.type
return gson.toJson(value, type)
}
@TypeConverter
fun toQuestionLimit(value: String): QuestionLimit {
val type = object : TypeToken<QuestionLimit >() {}.type
return gson.fromJson(value, type)
}
// CONTINENT
@TypeConverter
fun fromContinentList(value: List<Continent>): String {
val type = object : TypeToken<List<Continent>>() {}.type
return gson.toJson(value, type)
}
@TypeConverter
fun toContinentList(value: String): List<Continent> {
val type = object : TypeToken<List<Continent>>() {}.type
return gson.fromJson(value, type)
}
// GAME MODE SET
@TypeConverter
fun fromGameModeSet(value: Set<GameMode>): String {
val type = object : TypeToken<Set<GameMode>>() {}.type
return gson.toJson(value, type)
}
@TypeConverter
fun toGameModeSet(value: String): Set<GameMode> {
val type = object : TypeToken<Set<GameMode>>() {}.type
return gson.fromJson(value, type)
}
}
</code>
<code>class Converters { private val gson = Gson() // SCORE @TypeConverter fun fromScore(value: User.Score): String { return gson.toJson(value) } @TypeConverter fun toScore(value: String): User.Score { return gson.fromJson(value, User.Score::class.java) } // SETTINGS @TypeConverter fun fromSettings(value: List<User.Settings>): String { return gson.toJson(value) } @TypeConverter fun toSettings(value: String): List<User.Settings> { val type = object : TypeToken<List<User.Settings>>() {}.type return gson.fromJson(value, type) } // QUESTION LIMIT @TypeConverter fun fromQuestionLimit(value: QuestionLimit): String { val type = object : TypeToken<QuestionLimit>() {}.type return gson.toJson(value, type) } @TypeConverter fun toQuestionLimit(value: String): QuestionLimit { val type = object : TypeToken<QuestionLimit >() {}.type return gson.fromJson(value, type) } // CONTINENT @TypeConverter fun fromContinentList(value: List<Continent>): String { val type = object : TypeToken<List<Continent>>() {}.type return gson.toJson(value, type) } @TypeConverter fun toContinentList(value: String): List<Continent> { val type = object : TypeToken<List<Continent>>() {}.type return gson.fromJson(value, type) } // GAME MODE SET @TypeConverter fun fromGameModeSet(value: Set<GameMode>): String { val type = object : TypeToken<Set<GameMode>>() {}.type return gson.toJson(value, type) } @TypeConverter fun toGameModeSet(value: String): Set<GameMode> { val type = object : TypeToken<Set<GameMode>>() {}.type return gson.fromJson(value, type) } } </code>
class Converters {

    private val gson = Gson()

    // SCORE
    @TypeConverter
    fun fromScore(value: User.Score): String {
        return gson.toJson(value)
    }

    @TypeConverter
    fun toScore(value: String): User.Score {
        return gson.fromJson(value, User.Score::class.java)
    }

    // SETTINGS
    @TypeConverter
    fun fromSettings(value: List<User.Settings>): String {
        return gson.toJson(value)
    }

    @TypeConverter
    fun toSettings(value: String): List<User.Settings> {
        val type = object : TypeToken<List<User.Settings>>() {}.type
        return gson.fromJson(value, type)
    }

    // QUESTION LIMIT
    @TypeConverter
    fun fromQuestionLimit(value: QuestionLimit): String {
        val type = object : TypeToken<QuestionLimit>() {}.type
        return gson.toJson(value, type)
    }

    @TypeConverter
    fun toQuestionLimit(value: String): QuestionLimit {
        val type = object : TypeToken<QuestionLimit >() {}.type
        return gson.fromJson(value, type)
    }

    // CONTINENT
    @TypeConverter
    fun fromContinentList(value: List<Continent>): String {
        val type = object : TypeToken<List<Continent>>() {}.type
        return gson.toJson(value, type)
    }

    @TypeConverter
    fun toContinentList(value: String): List<Continent> {
        val type = object : TypeToken<List<Continent>>() {}.type
        return gson.fromJson(value, type)
    }

    // GAME MODE SET
    @TypeConverter
    fun fromGameModeSet(value: Set<GameMode>): String {
        val type = object : TypeToken<Set<GameMode>>() {}.type
        return gson.toJson(value, type)
    }

    @TypeConverter
    fun toGameModeSet(value: String): Set<GameMode> {
        val type = object : TypeToken<Set<GameMode>>() {}.type
        return gson.fromJson(value, type)
    }
}

The database entity:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Database(entities = [User::class], version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
@Volatile
private var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"app_database"
).build()
INSTANCE = instance
instance
}
}
}
}
</code>
<code>@Database(entities = [User::class], version = 1, exportSchema = false) @TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { abstract fun userDao(): UserDao companion object { @Volatile private var INSTANCE: AppDatabase? = null fun getDatabase(context: Context): AppDatabase { return INSTANCE ?: synchronized(this) { val instance = Room.databaseBuilder( context.applicationContext, AppDatabase::class.java, "app_database" ).build() INSTANCE = instance instance } } } } </code>
@Database(entities = [User::class], version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao

    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getDatabase(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "app_database"
                ).build()
                INSTANCE = instance
                instance
            }
        }
    }
}

Query giving the error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> @androidx.room.Query(value = "UPDATE user_table SET settings = :settings WHERE id = :id")
@org.jetbrains.annotations.Nullable
public abstract java.lang.Object updateSettings(int id, @org.jetbrains.annotations.NotNull
</code>
<code> @androidx.room.Query(value = "UPDATE user_table SET settings = :settings WHERE id = :id") @org.jetbrains.annotations.Nullable public abstract java.lang.Object updateSettings(int id, @org.jetbrains.annotations.NotNull </code>
    @androidx.room.Query(value = "UPDATE user_table SET settings = :settings WHERE id = :id")
    @org.jetbrains.annotations.Nullable
    public abstract java.lang.Object updateSettings(int id, @org.jetbrains.annotations.NotNull

The DAO:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun setUser(user: User)
@Query("SELECT * FROM user_table WHERE id = :id LIMIT 1")
suspend fun getUserById(id: Int): User?
@Query("UPDATE user_table SET settings = :settings WHERE id = :id")
suspend fun updateSettings(id: Int, settings: User.Settings)
@Query("UPDATE user_table SET score = :score WHERE id = :id")
suspend fun updateScore(id: Int, score: User.Score)
}
</code>
<code>@Dao interface UserDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun setUser(user: User) @Query("SELECT * FROM user_table WHERE id = :id LIMIT 1") suspend fun getUserById(id: Int): User? @Query("UPDATE user_table SET settings = :settings WHERE id = :id") suspend fun updateSettings(id: Int, settings: User.Settings) @Query("UPDATE user_table SET score = :score WHERE id = :id") suspend fun updateScore(id: Int, score: User.Score) } </code>
@Dao
interface UserDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun setUser(user: User)

    @Query("SELECT * FROM user_table WHERE id = :id LIMIT 1")
    suspend fun getUserById(id: Int): User?

    @Query("UPDATE user_table SET settings = :settings WHERE id = :id")
    suspend fun updateSettings(id: Int, settings: User.Settings)

    @Query("UPDATE user_table SET score = :score WHERE id = :id")
    suspend fun updateScore(id: Int, score: User.Score)
}

4

Your DAO function is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>suspend fun updateSettings(id: Int, settings: User.Settings)
</code>
<code>suspend fun updateSettings(id: Int, settings: User.Settings) </code>
suspend fun updateSettings(id: Int, settings: User.Settings)

This takes a single User.Settings. Given the rest of your code, this might need to take a List<User.Settings>, to line up with your entity and type converter.

If you are certain that you want a single User.Settings here, you probably need a converter for a single User.Settings, akin to what you have for User.Score. Right now, your type converter only converts a List<User.Settings>.

Recognized by Mobile Development Collective

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật