我收到以下错误:
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/