Home > OS >  How to resolve "Entities and POJOs must have a usable public constructor"
How to resolve "Entities and POJOs must have a usable public constructor"

Time:11-14

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)
    }
}

CodePudding user response:

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)
  • Related