我正在使用Android中的房间持久性数据库,但出现此错误 错误:参数的类型必须是用@Entity注释的类或它的集合/数组。 test.android.orca.com.ws2db.Models.User ...用户); 请帮助我
这是我的DAO:
@Dao
abstract interface UserDao
{
@get:Query("SELECT * FROM user")
val getall: List<User>
@Query("SELECT * FROM user where name LIKE :name AND login LIKE :login")
fun findByName(name: String, login: String): User
@Query("SELECT COUNT(*) from user")
fun countUsers(): Int
@Insert
fun insertAll(users: List<User>)
@Delete
fun delete(vararg user: User)
}
这是我的实体
@Entity(tableName = "user")
class UserTable {
@PrimaryKey(autoGenerate = true)
var uid: Int = 0
@ColumnInfo(name = "name")
var name: String? = null
@ColumnInfo(name = "login")
var login: String? = null
@ColumnInfo(name = "id")
var id: Int = 0
@ColumnInfo(name="url")
var url: String? = null
@ColumnInfo(name="company")
var company:String?=null
}
数据库
@Database(entities = [UserTable::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
private var INSTANCE: AppDatabase? = null
fun getAppDatabase(context: Context): AppDatabase? {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.applicationContext,
AppDatabase::class.java, "user-database")
.allowMainThreadQueries()
.build()
}
return INSTANCE
}
fun destroyInstance() {
INSTANCE = null
}
}}
答案 0 :(得分:1)
您正在混合user
和UserTable
。 User
对于引用您的表有效,但您的实体为UserTable
@Dao
abstract interface UserDao
{
@get:Query("SELECT * FROM user")
val getall: List<UserTable>
@Query("SELECT * FROM user where name LIKE :name AND login LIKE :login")
fun findByName(name: String, login: String): UserTable
@Query("SELECT COUNT(*) from user")
fun countUsers(): Int
@Insert
fun insertAll(users: List<UserTable>)
@Delete
fun delete(vararg user: UserTable)
}