Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room error: Type of the parameter must be a class annotated with @Entity or a collection/array of it

I'm getting the following error:

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

on my matchedBefore() query:

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

Here is my Entity

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

DB

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

    abstract fun matchedUsersDao(): MatchedUsersDao
}

and I instantiate my DB in my Application:

class CustomApplication : Application() {

    companion object {
        var database: AppDatabase? = null
    }

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

Can someone tell me what the error is indicating and how I can solve it?

like image 427
Zorgan Avatar asked Aug 25 '19 07:08

Zorgan


1 Answers

Your problem is this line

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

If you want use @Insert annotations you must send instance of MatchedUser not String.

So you can :

1) Send new MatchedUser with your id (insert empty MatchedUser with your id)

You code must be like this

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

2) Wrote new @Query for inserting just id in db

like image 179
Radesh Avatar answered Sep 26 '22 20:09

Radesh