Search code examples
androidkotlinandroid-room

Dao class must be annotated with @Dao - java.util.List


How I resolve this problem in my project Despite in Dao Class exist annotated @Dao on interface

notes.kt

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.ColumnInfo


@Entity(tableName="notes")
 data class Notes(

 @PrimaryKey(autoGenerate = true)
    var id:Int?= null,

 @ColumnInfo(name="title")
    var title:String,

 @ColumnInfo(name="description")
    var description:String



NotesDao.kt


import androidx.room.Dao
import androidx.room.Query


@Dao
interface NotesDao {

   @Query("SELECT * FROM notes") //name of table
    fun getAll():MutableList<Notes>  


}

NotesDatabase.kt


import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [Notes::class], version = 1)
abstract class NotesDatabase : RoomDatabase(), NotesDao {


    abstract fun getNotesDao():NotesDao 

    companion object{
        fun buldDatabase(context: Context)=
            Room.databaseBuilder(context.applicationContext,NotesDatabase::class.java,"notes.db")
            .allowMainThreadQueries().build()
    }

}

The version of the ROOM library is 2.5.1 Is there a problem with the database class?


Solution

  • I think NotesDatabase should not implement NotesDao interface. Remove NotesDao from the class declaration so it looks like this:

    abstract class NotesDatabase : RoomDatabase() {