17

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);
}
2
  • 2
    Where is the error message coming from? Does your RoomDatabase subclass perhaps have an abstract method that is returning something other than DeviceDAO? Commented Nov 6, 2017 at 23:15
  • 1
    That's an interface, not a class. Commented Nov 6, 2017 at 23:40

11 Answers 11

38

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

Sign up to request clarification or add additional context in comments.

2 Comments

I'm sure that I've wrote public abstract DeviceDao deviceDao(); Otherwise it would gave me an other error message. But I'm using now ObjectBox and it works. Anyhow thank you for your help.
Had the same issue. Reason was in the Database abstract class I have return the Entity class (not the DAO).
13

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

Comments

3

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.

Comments

2

For Kotlin users :

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

abstract val myDatabaseDao:MyDatabaseDao

Comments

1

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

Comments

1

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

Comments

0

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.

1 Comment

They were all imported and also have reimport them. And after this didn't work I have tried to rebuild my project but this didn't work, too. But now I'm using ObjectBox instead of Room and it works. Nevertheless thank you for your help.
0

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

Comments

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.