【问题标题】:Room error: Type of the parameter must be a class annotated with @Entity or a collection/array of it房间错误:参数的类型必须是带有@Entity 注释的类或其集合/数组
【发布时间】:2019-12-29 20:04:49
【问题描述】:

我收到以下错误:

error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
    java.lang.String matchId);

在我的matchedBefore() 查询中:

@Dao
interface MatchedUsersDao {

    // Checks to see if user has matched before (if it returns 1)
    @Query("SELECT COUNT(*) FROM matched_users WHERE :matchId = match_id LIMIT 1")
    fun matchedBefore(matchId: String): Int

    @Insert(onConflict = OnConflictStrategy.ABORT)
    fun addMatchUid(matchId: String)
}

这是我的实体

@Entity(tableName = "matched_users")
data class MatchedUser(
    @PrimaryKey(autoGenerate = true) val id: Int,
    @ColumnInfo(name = "match_id") val matchId: String
)

数据库

@Database(entities = arrayOf(MatchedUser::class), version = 1)
abstract class AppDatabase : RoomDatabase() {

    abstract fun matchedUsersDao(): MatchedUsersDao
}

我在我的应用程序中实例化我的数据库:

class CustomApplication : Application() {

    companion object {
        var database: AppDatabase? = null
    }

    override fun onCreate() {
        super.onCreate()
        CustomApplication.database = Room.databaseBuilder(this, AppDatabase::class.java, "AppDatabase").build()
    }

谁能告诉我这个错误是什么意思以及我该如何解决?

【问题讨论】:

    标签: android kotlin android-room


    【解决方案1】:

    你的问题是这一行

    @Insert(onConflict = OnConflictStrategy.ABORT)
    fun addMatchUid(matchId: String)
    

    如果你想使用@Insert 注释,你必须发送MatchedUser 的实例而不是字符串。

    所以你可以:

    1) 使用您的 id 发送新的 MatchedUser(使用您的 id 插入空的 MatchedUser

    你的代码一定是这样的

    @Insert(onConflict = OnConflictStrategy.ABORT)
    fun addMatchUid(matchUser: MatchedUser)
    

    2) 写了新的@Query 用于在db中插入id

    【讨论】:

      猜你喜欢
      • 2018-07-05
      • 2022-12-15
      • 2020-09-29
      • 2021-08-27
      • 2021-12-29
      • 2022-01-11
      • 2019-10-19
      • 1970-01-01
      • 2018-06-09
      相关资源
      最近更新 更多