gpt4 book ai didi

java - Room Android 实体和 POJO 必须具有可用的公共(public)构造函数

转载 作者:行者123 更新时间:2023-12-01 16:41:19 31 4
gpt4 key购买 nike

我正在尝试将数据插入数据库,但总是出现下面提到的编译时错误,我缺少什么吗?

我遇到错误

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

用户响应模型类

@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 class Data(

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

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

用户信息模型类

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)

}

我的数据库

@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>

据我所知,您不能使用 @Embedded用于任意事物,其中包括列表。

将其更改为使用关系或使用 @TypeConverter @TypeConverters 转换您的List<Mediafile>转换为一些有效的列类型,例如 String .

关于java - Room Android 实体和 POJO 必须具有可用的公共(public)构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61857208/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com