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

5

7

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.

Tridimensional answered 8/6, 2021 at 20:17 Comment(5)
try to add return type return type in insert method suspend fun insert(note :Note) : LongInitiate
Plead add to code snippets imports section, maybe you accidentally import wrong class with same name but other packageFissionable
@MuhammadAhmed thanks for the reply can you explain why should I add a return type to an Insert function as there is nothing to return as that function inserts a row in my entity. Still I tried your and unfortunately it didn't worked and I received the same errorTridimensional
There is something wrong in your DAO. try to remove all methods from your DAO and run your appInitiate
@MuhammadAhmed I guess there was some issues with the room version that I added in my dependencies I updated it and now it's working fine thanks for your help.Tridimensional
T
5

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.

Tridimensional answered 10/6, 2021 at 11:21 Comment(1)
it was something to do with room version - it would have been better if you were clear about the details so your answer would help people with similar problemsPublic
M
3

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

Maccabees answered 18/11, 2021 at 20:40 Comment(0)
J
3

It's Working for me to update the Room version: 2.4.2

   implementation "androidx.room:room-runtime:2.4.2"
   kapt "androidx.room:room-compiler:2.4.2"
   implementation "androidx.room:room-ktx:2.4.2"
   androidTestImplementation "androidx.room:room-testing:2.4.2"
Jimmie answered 10/3, 2022 at 10:56 Comment(0)
C
2

Fixed by raising room version to "2.4.0-beta01"

Cerotype answered 6/11, 2021 at 8:47 Comment(0)
V
1

October 2023

kapt annotation processing tool didn't work for me with suspend functions.

What I did to fix it:

root build.gradle

buildscript {
    ext.room_version = '2.5.2'
}
plugins {
    id 'com.android.application' version '8.1.2' apply false
    //kotlin gradle plugins
    id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
    id 'com.google.devtools.ksp' version '1.9.0-1.0.13' apply false
}

I noticed that kotlin gradle plugins version has to match ksp plugin version or it does not work(ksp version 1.8 didn't work for me).

app/build.gradle

plugins {
    //id 'kotlin-kapt'
    id "com.google.devtools.ksp"
}

dependencies {
    implementation "androidx.room:room-runtime:$room_version"
    ksp "androidx.room:room-compiler:$room_version"
    //DOES NOT WORK
    //kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
}
Votaw answered 8/10, 2023 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.