2

I have seen this question several times on SO. however the solution doesn't seem to apply to my problem.

I have a Kotlin data-class that is used as an Entity in Room

@Entity(tableName = "training_session")
data class SessionEntity(
    @PrimaryKey(autoGenerate = false) val id: Long,
    @ColumnInfo(name = "current_state_marker") val currentState: Short,
    @Embedded val states: List<Int>
)

It is producing

> Task :training-infrastructure:kaptDebugKotlin FAILED
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - java.util.List
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - java.util.List

In the same project I have a very similar entity which also has a list and that doesn't produce any errors.


Tried out the answer provided by MikeT, for me it required a small change in the way the converters were defined

data class SessionStateList (val stateList : List<Int>) 

class SessionStateListConverter {

    @TypeConverter
    fun fromArraySessionStateList(sh: List<Int>?): String? {
        return Gson().toJson(sh)
    }
    @TypeConverter
    fun toArraySessionStateList(sh: String?): List<Int>? {
        val listType: Type = object : TypeToken<ArrayList<Int?>?>() {}.type
        return Gson().fromJson(sh,listType)
    }
}

A quick follow-up. I had mentioned that I have another Entity that has an Embedded val something: List<Int> and I had not noticed any compiler errors.

The reason, I had not noticed any compiler errors was because the entity was not included in the @Database annotation.

hba
  • 7,406
  • 10
  • 63
  • 105
  • A simple fix for the immediate error might be to use an `ArrayList`. `List` is an interface and has no constructor. “error: Entities .. must have a usable public constructor .. - java.util.List” – user2864740 Nov 13 '21 at 22:14
  • 1
    Do you have a `TypeConverter` registered on the `RoomDatabase` that converts `List` to something that Room can use? – CommonsWare Nov 13 '21 at 22:17
  • 1
    Although getting to the “end goal” probably involves some mapping to meaningfully associate a list to an SQL table.. see https://stackoverflow.com/questions/44986626/android-room-database-how-to-handle-arraylist-in-an-entity – user2864740 Nov 13 '21 at 22:18
  • 1
    @user2864740 - good point - that escaped me, but changing it created another issue "error: Cannot find setter for field. - size in java.util.ArrayList". Also the link you provided proved to be helpful in mapping the array - thanks – hba Nov 14 '21 at 04:06

1 Answers1

3

You cannot have a List/Array etc as a column type. So your issue is centred on @Embedded val states: List<Int>

You could have a POJO e.g. StatesHolder :-

data class StatesHolder(
    val stateList: List<Int>
)

and then have

@Entity(tableName = "training_session")
data class SessionEntity(
    @PrimaryKey(autoGenerate = false) val id: Long,
    @ColumnInfo(name = "current_state_marker") val currentState: Short,
    val states: StatesHolder
)
  • Note that you cannot Embed StatesHolder as then that just inserts List. If you want to Embed then you have to Embed a wrapper that uses a StatesHolder.

You will then need TypeConverters to convert to and from a StatesHolder object to a type that can be stored. Probably a String and Probably a JSON respresentation of the StatesHold object e.g.

class Converters {

    @TypeConverter
    fun fromStatesHolder(sh: StatesHolder): String {
        return Gson().toJson(sh)
    }
    @TypeConverter
    fun toStatesHolder(sh: String): StatesHolder {
        return Gson().fromJson(sh,StatesHolder::class.java)
    }
}

You additionally need to use @TypeConverters annotation that defines the Converts::class. If coded at the @Database level the converters have full scope.

So after @Database(.....) you could have :-

@TypeConverters(Converters::class)
MikeT
  • 51,415
  • 16
  • 49
  • 68