Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am new to android development and I am trying to make a notes app by following the android architecture components but on running I am getting errors in my DAO if any one could help would be highly grateful. Here's the code and the error that I am getting.

Error That I am getting

DAO:-

'''

@Dao
interface NoteDao {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(note :Note)

    @Delete
    suspend fun delete(note : Note)

    @Query("SELECT * FROM Notes_table order by id")
    fun getALL(): LiveData<List<Note>>

    @Query("SELECT * From Notes_table where id= :pos")
    fun getSpecific(pos :Int):Note

}

'''

Entity:-

'''

@Entity(tableName = "Notes_table")
data class Note(@ColumnInfo(name="noteText") val text:String) {
    @PrimaryKey(autoGenerate = true) var id:Int =0

}

''' Database:-

'''

@Database(entities = [Note::class],version = 1,exportSchema = false)
abstract class NoteDatabase : RoomDatabase() {

    abstract fun getNoteDao():NoteDao

    companion object{
        @Volatile
        private var Instance: NoteDatabase?=null

        fun getDatabase(context :Context):NoteDatabase{

            return Instance ?: synchronized(this){
                val instance=Room.databaseBuilder(context.applicationContext,
                NoteDatabase::class.java,"note_database").build()
                Instance=instance
                instance
            }
        }

    }

}

Imports For DAO:-

import androidx.lifecycle.LiveData
import androidx.room.

Imports for Entity:-

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

Imports for Database:-

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

I can provide the rest of the code if required.

like image 333
Tushar Verma Avatar asked Jan 25 '23 08:01

Tushar Verma


2 Answers

With the help in the comment section and by reading the documentation I figured that something was wrong with my DAO and eventually it was something to do with the ROOM Version that I added to my dependencies I just updated those and now it's working fine.

like image 130
Tushar Verma Avatar answered Feb 04 '23 02:02

Tushar Verma


Got the same problem today, fixed by updating Room version from 2.3.0 to 2.4.0-beta02. Version can be found here: https://developer.android.com/jetpack/androidx/releases/room#2.3.0-alpha04

like image 45
Sam Chen Avatar answered Feb 04 '23 04:02

Sam Chen