I have 2 tables in one database both are completely separate (no relation between them), I want to store data in room database which has List and this List has other data fields which also includes List<sub_data>. Here is my all data classes.
ChapterSlok class:
@Entity(tableName = "slok")
data class ChapterSlok(
@PrimaryKey
val chapter_id: Int,
val slokList: List<ChapterSlokItem>
)
ChapterSlokItem Class:
data class ChapterSlokItem(
val chapter_number: Int,
val commentaries: List<Commentary>,
val id: Int,
val slug: String,
val text: String,
val translations: List<Translation>,
val transliteration: String,
val verse_number: Int,
val word_meanings: String
)
Commentary class:
data class Commentary(
val author_name: String,
val description: String,
val id: Int,
val language: String
)
Translation class:
data class Translation(
val author_name: String,
val description: String,
val id: Int,
val language: String
)
Here is my DAO interface:
@Dao
interface ChapterSummaryDAO {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addChapterSummary(chapters: List<ChapterSummaryItem>)
@Query("SELECT * FROM chapter")
suspend fun getChapterSummary(): List<ChapterSummaryItem>
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addChapterSlok(slok: ChapterSlok)
@Query("SELECT * FROM slok")
suspend fun getChapterSlok(): List<ChapterSlok>
}
And here is my Database:
@Database(entities = [ChapterSummaryItem::class, ChapterSlok::class], version = 1)
@TypeConverters(Converters::class)
abstract class ChapterSummaryDataBase : RoomDatabase() {
abstract val chapterSummaryDAO: ChapterSummaryDAO
companion object {
@Volatile
private var INSTANCE: ChapterSummaryDataBase? = null
fun getDataBase(context: Context): ChapterSummaryDataBase {
if (INSTANCE == null) {
synchronized(this) {
INSTANCE = Room.databaseBuilder(
context,
ChapterSummaryDataBase::class.java,
"chapterDB"
).build()
}
}
return INSTANCE!!
}
}
}
Here is my Typeconverter Class:
@ProvidedTypeConverter
class Converters {
val gson = Gson()
@TypeConverter
fun fromCommentariesList(commentaries: List<Commentary>): String {
return gson.toJson(commentaries)
}
@TypeConverter
fun toCommentariesList(commentariesString: String): List<Commentary> {
val type = object : TypeToken<List<Commentary>>() {}.type
return gson.fromJson(commentariesString, type)
}
@TypeConverter
fun fromTranslationsList(translations: List<Translation>): String {
return gson.toJson(translations)
}
@TypeConverter
fun toTranslationsList(translationsString: String): List<Translation> {
val type = object : TypeToken<List<Translation>>() {}.type
return gson.fromJson(translationsString, type)
}
@TypeConverter
fun fromChapterSlokItemList(slokList: List<ChapterSlokItem>): String {
return gson.toJson(slokList)
}
@TypeConverter
fun toChapterSlokItemList(chapterSlokString: String): List<ChapterSlokItem> {
val type = object : TypeToken<List<ChapterSlokItem>>() {}.type
return gson.fromJson(chapterSlokString, type) }
}
The error is
Unable to create application com.practice.gita.GitaApplication: java.lang.IllegalArgumentException: A required type converter (class com.practice.gita.Converters.Converters) for com.practice.gita.db.ChapterSummaryDAO is missing in the database configuration.
How i can convert this datatypes so it can be stored in Room. Can anyone please help me to modify type converters. As I don't need Commentary or Translation without it's whole ChapterSlok, I tried to @Serializable and encoded to string and decoded to class too. but still getting same error.