【问题标题】:Kotlin Room error : entities and pojos should have a constructorKotlin Room 错误:实体和 pojo 应该有一个构造函数
【发布时间】:2019-11-11 06:54:51
【问题描述】:

请不要将此标记为重复我看过其他类似的帖子,但没有任何帮助

我的实体:

@Entity(tableName = "batch_table")
data class Batch(
    val batch_id: String? ="",
    val batch_name: String? ="",
    val user_m_id: String? ="",
    val user_profile_id: String? =""
){
    @PrimaryKey(autoGenerate = true)
    var id1: Int? = 0
constructor():this("","","","")
}

我的道:

@Dao
interface BatchDao{

    @Insert
    suspend fun insert(batch : MutableList<Batch>)

    @Query("delete from batch_table")
    suspend fun deleteBatchTable()

    @Query(" select * from batch_table ")
    suspend fun getAllBatches() :List<Batch>

    @Query("select batch_name from batch_table where batch_id = :batch_id")
    suspend fun getBatchName(batch_id:String)

    @Transaction
    suspend fun insertBatches(batch: MutableList<Batch>){
        deleteBatchTable()
        insert(batch)
    }

我使用的是room,我所做的只是在我的dao 中添加第四个函数(getBatchName),但我不确定为什么会开始出现此错误。

编译时的错误:

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). - `kotlin.Unit`

【问题讨论】:

标签: android kotlin android-room


【解决方案1】:

我忘了写函数的返回类型,但是抛出的错误指向了不同的东西,所以我很困惑。

 @Query("select batch_name from batch_table where batch_id = :batch_id")
    suspend fun getBatchName(batch_id:String) : String

.

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 2020-09-03
    • 1970-01-01
    • 2018-10-19
    • 2021-05-01
    • 1970-01-01
    • 2017-11-20
    • 2019-05-21
    • 1970-01-01
    相关资源
    最近更新 更多