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);
}
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.
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 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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With