问题描述
我正在使用Room架构组件来实现持久性。我已经创建了通用DAO接口以避免样板代码。
I am using Room architecture component for persistence. I have created generic DAO interface to avoid boilerplate code. Room Pro Tips
但是我的代码没有编译说错误:(21,19)错误:参数的类型必须是用@Entity或它的集合/数组注释的类。对于通用类T。
But my code doesn't compile saying "Error:(21, 19) error: Type of the parameter must be a class annotated with @Entity or a collection/array of it." for the Generic class T.
interface BaseDao<T> {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(T... entity);
@Update
void update(T entity);
@Delete
void delete(T entity);
}
@Dao
public abstract class ReasonDao implements BaseDao<ReasonDao> {
@Query("SELECT * from Reason")
abstract public List<Reason> getReasons();
}
这里有什么我想念的。
它就像这样
Is there anything I am missing here. It works like this here
推荐答案
我最初遵循Kotlin中使用的方法,但这给出了Java代码中的错误。
两个快速更改为我修复了
I had initially followed the method used in Kotlin, but that gives the error in Java code. Two quick changes fixed it for me
- 将BaseDao更改为抽象类
- 已添加@Dao注释到BaseDao
请找到下面的代码,现在它正常运行
Please find the code below and now it runs properly
@Dao
abstract class BaseDao<T> {
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract void insert(T entity);
@Update
abstract void update(T entity);
@Delete
abstract void delete(T entity);
}
@Dao
public abstract class ReasonDao extends BaseDao<Reason>{
@Query("SELECT * from Reason")
abstract public List<Reason> getReasons();
}
这篇关于“参数的类型必须是用@ Entity注释的类”。在Room中创建Generic DAO接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!