0

I'm working with Room persistance databases in android but i get this error error: Type of the parameter must be a class annotated with @Entity or a collection/array of it. test.android.orca.com.ws2db.Models.User... user); Please help me

This is my 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)
}

This is my Entity

@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

@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
    }
}}

This is the error : https://i.sstatic.net/FnECR.jpg

1 Answer 1

1

You are mixing user and UserTable. User is valid for referencing your table, but your entity is 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)
}
Sign up to request clarification or add additional context in comments.

3 Comments

val getall: List<User> also should be val getall: List<UserTable> ?
Glad to help. However, let me advise you to change some names. It's better to change UserTable to User_entity and user to user_table. I think it's more readable.
So, If I want to use model converter to entity by typeConverter How can I do that? Example, When I have a UserEntity and UserModel, I can want to save UserModel, And I want to converting by Model to Entity by @TypeConverter. In this case, compiler is throwing "Type of the parameter must be a class annotated with @Entity"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.