Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 754
Prizrenali Avatar asked Nov 06 '17 23:11

Prizrenali


People also ask

What is Dao in Android?

See androidx.room.Dao instead. Marks the class as a Data Access Object. Data Access Objects are the main classes where you define your database interactions. They can include a variety of query methods.

What is a @dao class?

They can include a variety of query methods. The class marked with @Dao should either be an interface or an abstract class. At compile time, Room will generate an implementation of this class when it is referenced by a Database . An abstract @Dao class can optionally have a constructor that takes a Database as its only parameter.

Does roomdatabase subclass have an abstract method other than devicedao?

Does your RoomDatabase subclass perhaps have an abstract method that is returning something other than DeviceDAO? That's an interface, not a class. Check your database class. When you define DAO, you must have use wrong type (Device instead of DeviceDAO). Hope this will work. Thanks

What are daos in room persistence?

When you use the Room persistence library to store your app's data, you interact with the stored data by defining data access objects, or DAOs. Each DAO includes methods that offer abstract access to your app's database.


1 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

like image 92
Ishara Meegahawala Avatar answered Oct 12 '22 23:10

Ishara Meegahawala