Home » Android » java – Android Room error: Dao class must be annotated with @Dao

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

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);
}
How to&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

Answer:

Error Message:
Dao class must be annotated with @Dao

To solve error please read it properly.

If this error messages shows on Model class then you need to modify your AppDatabase class. I am giving you the code what gives error then error corrected code.

Error Code:

MyImage.java

@Entity
public class MyImage {
    @PrimaryKey(autoGenerate = true)
    private int uid;
    @ColumnInfo(name = "title")
    private String title;
    @ColumnInfo(name = "photo")
    private String photo;

    public MyImage(String title, String photo) {
        this.title = title;
        this.photo = photo;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }
}

MyImageDao.java

@Dao
public interface MyImageDao {
    @Query("SELECT * FROM myimage")
    List<MyImage> getAll();

    @Insert
    void insertAll(MyImage... myImages);

    @Delete
    void delete(MyImage myImage);
}

AppDatabase.java

@Database(entities = {MyImage.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    public abstract MyImage myImageDao();
}

Here has error on only AppDatabase.java file, you can see myImageDao has return type MyImage, that means it assumed that MyImage is a Dao class but MyImage is model class and MyImageDao is Dao class.
So it need to modify AppDatabase.java class and MyImage to MyImageDao.

The corrected code is-

AppDatabase.java

@Database(entities = {MyImage.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    public abstract MyImageDao myImageDao();
}

Answer:

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

Answer:

For Kotlin users :

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

abstract val myDatabaseDao:MyDatabaseDao

Answer:

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.

Answer:

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

Answer:

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

Answer:

@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.