起初,我知道这是duplicate question,但标记为答案不能解决错误。
我正在尝试在this guide之后在我的房间持久性库中创建通用的Dao接口,但是无法解决错误“参数的类型必须是带有@Entity或其集合/数组注释的类” GenericDao中的通用 T 。
GenericDao:
@Dao
public interface GenericDao<T> {
@Insert
long insert(T object);
@Update
void update(T... objects);
@Delete
void delete(T... objects);
}
继承道:
@Dao
public abstract class ExerciseDao implements GenericDao<Exercise> {
@Query("SELECT * FROM exercises")
public abstract LiveData<List<Exercise>> getAllExercises();
}
实体:
@Entity(tableName = "exercises",
indices = {@Index(value = {"name"}, unique = true)})
public class Exercise {
@PrimaryKey (autoGenerate = true)
int id;
@ColumnInfo(name = "name")
private String name;
@ColumnInfo(name = "muscle_part")
private int musclePart;
@ColumnInfo(name = "is_shown")
private boolean isShown;
public Exercise(String name, int musclePart, boolean isShown) {
this.name = name;
this.musclePart = musclePart;
this.isShown = isShown;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMusclePart() {
return musclePart;
}
public void setMusclePart(int musclePart) {
this.musclePart = musclePart;
}
public boolean isShown() {
return isShown;
}
public void setShown(boolean shown) {
isShown = shown;
}
}
我尝试过:
但是总是会出现错误。 有谁知道如何解决这个问题?