android - 房间错误 : Type of the parameter must be a class annotated with @Entity or a collection/array of it

标签 android kotlin android-room

我收到以下错误:

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

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

最佳答案

你的问题是这一行

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

如果您想使用 @Insert 注释,您必须发送 MatchedUser 的实例,而不是 String。

所以你可以:

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

你的代码必须是这样的

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

2) 编写了新的 @Query 用于在数据库中插入 id

关于android - 房间错误 : Type of the parameter must be a class annotated with @Entity or a collection/array of it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57644212/

相关文章:

android - 拆分 APK 后我应该选择哪一个?

java - 在Eclipse上创建两个Android项目

java - 安卓编译器: Cannot resolve symbol string

android - 如何在 kotlin parcelable 中使用 An object?

java - 为什么 android room 在生成它的异步任务(对象)的末尾插入一个对象?

android - 如何在Android中制作Circular recyclerview(带有滚动的布局)?

安卓谷歌登录

java - 在 List 上调用 .sort 时出现 AbstractList UnsupportedOperationException

android - 如何针对特殊的 "many-to-many relationship"用例查询 Android Room CrossReference 实体中的数据?

java - 如何使在主线程中运行的某个类同时等待其他类达到某种特定状态?