1

Here i need to store multiple custom entities in same table in room database. This things are working in single typeconverter but when it comes in the case of multiple typeconverters then it is throwing error.

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type


@Entity(tableName = "tbl_finalOrder")
@TypeConverters(ItemEntityConverter::class)
data class FinalOrdersEntity (
    @PrimaryKey(autoGenerate = true)
    val id:Int,

     val itemEntity: ItemEntity?,
   // val itemEntity: String? = null,
    var quantity: String?=null,
    var unitName: String?=null,
    var unitId: Int?=null,
    var statusOfScheme: Boolean?=null,

    @TypeConverters(SchemeDetailConverter::class)
    var listOfScheme: List<SchemeDetailEntity>?=null,
  
    val child_items: String?=null,
    val selectedOffer:String? = null,
    var offer:String?=null
)






class ItemEntityConverter {
    @TypeConverter
    fun stringToItemEntity(string: String?): ItemEntity  = Gson().fromJson(string,ItemEntity::class.java)

    @TypeConverter
    fun itemEntityToString(list: ItemEntity?): String =Gson().toJson(list)
}


class SchemeDetailConverter {
    @TypeConverter
    fun stringToSchemeDetailEntity(json: String?): List<SchemeDetailEntity> {
        val gson = Gson()
        val type: Type = object : TypeToken<List<SchemeDetailEntity?>?>() {}.type
        return gson.fromJson<List<SchemeDetailEntity>>(json, type)
    }

    @TypeConverter
    fun schemeDetailEntityToString(list: List<SchemeDetailEntity?>?): String {
        val gson = Gson()
        val type: Type = object : TypeToken<List<SchemeDetailEntity?>?>() {}.type
        return gson.toJson(list, type)
    }
}


When i run this then it ask to make typeconverter class for listOfScheme.However, there is one already for it.

error FinalOrdersEntity.java:22: error: Cannot figure out how to save this field into database. You can consider adding a type converter for it. private java.util.List<com.myproject.database.entities.SchemeDetailEntity> listOfScheme;

Can you tell me where and what i am missing here. Thank you in advance..

Lazy Mind
  • 143
  • 9

1 Answers1

-1

data class FinalOrdersEntity (
    @PrimaryKey(autoGenerate = true)
    val id:Int,
@TypeConverters(ItemEntityConverter::class)
     val itemEntity: ItemEntity?,
   // val itemEntity: String? = null,
    var quantity: String?=null,
    var unitName: String?=null,
    var unitId: Int?=null,
    var statusOfScheme: Boolean?=null,

    @TypeConverters(SchemeDetailConverter::class)
    var listOfScheme: List<SchemeDetailEntity>?=null,
  
    val child_items: String?=null,
    val selectedOffer:String? = null,
    var offer:String?=null
)

Check if the SchemeDetailEntity has any variable with class as the type,if yes then you should add the type converter class for that too

Rohini
  • 21
  • 5