Android Room error: Dao class must be annotated with @Dao

Viewed 15481

I'm using Room for my android app. I'm now trying to setup my database, but there is an error message, which says, that the Dao class must be annotated with @Dao. But as you can see in the coding snippet, the Dao class is annotated with @Dao. Does anyone know where the problem or my mistake could be? Both files aren't in the same folder (DAO is in the service folder while the other class is in the model folder)

Device.java

@Entity(tableName = "device")
public class Device {

    @PrimaryKey(autoGenerate = true)
    public int device_id;

    @ColumnInfo(name = "identifier")
    public String identifier;

    @ColumnInfo(name = "language")
    public int language;

    @ColumnInfo(name = "searchFilter")
    public int searchFilter;

    public Device(String identifier, int language, int searchFilter){
        this.identifier = identifier;
        this.language = language;
        this.searchFilter = searchFilter;
    }
}

DeviceDAO.java

@Dao
public interface DeviceDAO {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void addDevicePreferences(DifficultType difficultType);

    @Query("SELECT * FROM device")
    List<Device> selectAllDevicePreferences();

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void updateDevicePreferences(Device device);
}
9 Answers

Check your database class. When you define DAO, you must have use wrong type(Device instead of DeviceDAO).

Incorrect

public abstract Device deviceDao();

Correct

public abstract DeviceDAO deviceDao();

Hope this will work. Thanks

For Kotlin users :

Check if you've added following line in your Database file.

abstract val myDatabaseDao:MyDatabaseDao

I was facing the same issue, after struggling for some time I realized that in the database class, I created a variable of Entity class instead of Dao class.

Check if you have any additional methods in your interface. In my Kotlin implementation I had:

@Dao interface DeviceDao {
    @get:Query("SELECT * FROM extdevice")
    val all: List<ExtDevice>

    fun first() : ExtDevice? {
        val devices = all
        if (devices.isNotEmpty())
            return devices[0]
        return null
    }
}

removing first() solved my issue:

@Dao interface DeviceDao {
    @get:Query("SELECT * FROM extdevice")
    val all: List<ExtDevice>
}

add

import androidx.room.Dao;

to your interface that u set querys on it and then add the first line from this code

@Dao
public interface UserDeo {
    @Query("SELECT * FROM user")
    List<User> getAllUsers();

    @Insert
    void insertAll(User... users);
}

Your syntax look correct by what i can tell. Have you tried the following things:

  1. Are your imports complete?

    import android.arch.persistence.room.Dao; import android.arch.persistence.room.Delete; import android.arch.persistence.room.Insert; import android.arch.persistence.room.OnConflictStrategy; import android.arch.persistence.room.Query; import android.arch.persistence.room.Update;

Mabe delete them an reimport all.

  1. Have you rebuild / Gradel Build the project?

I did a Project with Room as well and i had no problems having the same syntax.

in my case, i have implement @Dao annotation and still get the error. the error is :

error: Dao class must be annotated with @Dao public final class NonExistentClass{ }

just make sure your room dependencies version same as the others, my room dependencies :

    kapt "androidx.room:room-compiler:2.2.0"
    implementation "androidx.room:room-runtime:2.2.0"
    implementation "androidx.room:room-ktx:2.2.0"

don't forget to use kapt instead of annotation processor and add :

apply plugin: 'kotlin-kapt'

above your build.gradle module app, because annotationProcessor will cause another errors, like database_impl.

then you should clean and build the project

hope it will help whoever see this

@Database(entities = {ObjInspectionSqlite.class}, version = 2, exportSchema = false)
@TypeConverters({InspeccionDateTypeConverter.class})
public abstract class DataBaseInspections extends RoomDatabase {

    public static BaseDeDatosInspecciones instance;

    public abstract InspectionsDao inspectionsDao();
    public abstract ObjInspectionSqlite inspectionSqlite();
    ...
}

note that ObjInspectionSqlite is a class whit @Entity , and I've declared it abstract in my DataBase

This will trow:

"error: Dao class must be annotated with @Dao"

even if you declared your dao correctly.

Maybe you declared your class as an abstract somewhere in your database and the DB expects it to be an abstract Dao that can be implemented.

Add code snippet of your DataBase since all answers point to a coding error in that class.

Related