简体   繁体   中英

Room Android Entities and POJOs must have a usable public constructor

I'm trying to insert data into database but always get compile time error mentioned below is there anything I'm missing?

Error i'm getting

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

User Response Model class

@Entity(tableName = "RoomDemo")
data class UserResponse(

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "code")
    @SerializedName("code")
    var code: Int,

    @Embedded
    @SerializedName("data")
    var `data`: Data,

    @Embedded
    @SerializedName("errors")
    var errors: Errors,

    @ColumnInfo(name = "message")
    @SerializedName("message")
    var message: String

) 

Data model class

data class Data(

    @ColumnInfo(name = "avater")
    @SerializedName("avater")
    var avater: String,

    @Embedded(prefix = "avater")
    @SerializedName("user_info")
    var userInfo: UserInfo
)

User Info Model class

data class UserInfo(
    @ColumnInfo(name = "location")
    @SerializedName("location")
    var location: String,

    @Embedded
    @SerializedName("mediafiles")
    var mediafiles: List<Mediafile>)

Dao Interface

@Dao
interface CrushDao {

    @Insert(onConflict = REPLACE)
    fun insert(userResponse: UserResponse)

}

My Database

@Database(entities = [UserResponse::class], version = 2)
abstract class CrushDataBase : RoomDatabase()
{
    abstract fun crushDao():CrushDao

    companion object{
        private var INSTANCE: CrushDataBase? = null

        fun getDatabase(context: Context): CrushDataBase? {
            if (INSTANCE == null) {
                synchronized(CrushDataBase::class) {
                    INSTANCE = Room.databaseBuilder(
                        context.getApplicationContext(),
                        CrushDataBase::class.java, "crushDemo.db"
                    ).build()
                }
            }
            return INSTANCE
        }
    }
}
@Embedded
@SerializedName("mediafiles")
var mediafiles: List<Mediafile>

AFAIK, you cannot use @Embedded for arbitrary things, which includes lists.

Either change this to use relations or use @TypeConverter and @TypeConverters to convert your List<Mediafile> into some valid column type, such as a String .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM