Prizrenali
Prizrenali

Reputation: 161

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

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

Upvotes: 16

Views: 19660

Answers (11)

Yiğithan Eryiğit
Yiğithan Eryiğit

Reputation: 1

You should make sure about the entities part in the @Database annotation you created in your Database class.

@Database(
   entities = [ImageToUpload::class, ImageToDeleteDao::class],
   version = 2,
   exportSchema = false
)

this is wrong part because i added dao class not model class eg: ImageToDelete

and i changed to "ImageToDelete::class" everything works fine

Upvotes: 0

Khaled hawwas
Khaled hawwas

Reputation: 1

in my case i deleted a Dao and forget to remove it from Database

    abstract fun testDao(): TestDao

the android studio and gradle didn't show my this error at all

Upvotes: 0

imok1948
imok1948

Reputation: 91

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.

Upvotes: 3

FranK
FranK

Reputation: 159

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

Upvotes: 0

makri aymen abderraouf
makri aymen abderraouf

Reputation: 828

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

Upvotes: 1

Shivam Sompura
Shivam Sompura

Reputation: 135

For Kotlin users :

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

abstract val myDatabaseDao:MyDatabaseDao

Upvotes: 2

icgoogo
icgoogo

Reputation: 491

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

Upvotes: 0

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

Upvotes: 1

Asif Mohammad Mollah
Asif Mohammad Mollah

Reputation: 442

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

Upvotes: 13

Andreas Imsand
Andreas Imsand

Reputation: 119

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.

Upvotes: 0

Ishara Meegahawala
Ishara Meegahawala

Reputation: 757

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

Upvotes: 37

Related Questions