"Type of the parameter must be a class annotated with @Entity" 在 Room 中创建通用 DAO 接口时
"Type of the parameter must be a class annotated with @Entity" while creating Generic DAO interface in Room
我正在使用 Room 架构组件来实现持久性。我创建了通用的 DAO 接口以避免样板代码。
Room Pro Tips
但是我的代码没有编译说 "Error:(21, 19) error: Type of the parameter must be a class annotated with @Entity or a collection/array of it." 通用 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();
}
这里有什么我遗漏的吗?
它是这样工作的 here
我最初是按照 Kotlin 中使用的方法,但在 Java 代码中给出了错误。
两个快速更改为我修复了它
- 将 BaseDao 更改为抽象 class
- 向 BaseDao 添加 @Dao 注释
请找到下面的代码,现在可以正常运行了
@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 class(包含@Entity(tableName = "your_table", indices = [Index("your_key")])
).
我相信是你错过了给 T class 的 Entity
注释。比如Reason
class 应该有@Entity
给ReasonDao class。喜欢:
@Dao
public abstract class ReasonDao extends BaseDao<Reason>{}
原因是您将 ReasonDao 类型指定为通用参数,而不是 Reason.
原代码:
@Dao
public abstract class ReasonDao implements BaseDao<ReasonDao> {
...
}
正确代码:
@Dao
public abstract class ReasonDao implements BaseDao<Reason> {
...
}
其中原因是标有@Entity[的类型=31=]注解.
顺便说一句,这已在接受的答案中修复,但未在更改列表中提及:)
What is missing here is the Data class. Generally,
@Entity represents objects you want to store,
- Room entity includes fields for each column inside the
- table in the database
@Entity(tableName="something") data class YourData()
对于那些在 dao 中使用 Kotlin 时出现“参数类型必须是用 @Entity 注释的 class 或它的 collection/array”错误的人,您应该尝试使用 @函数上的 JvmSuppressWildcards 注释。例如
@Query("SELECT * FROM materials")
@JvmSuppressWildcards
fun getAllMaterials(): LiveData<List<MaterialModel>>
在 gradle 中更改自:
kapt "androidx.room:room-compiler:$roomVersion"
对此:
annotationProcessor "androidx.room:room-compiler:$room_version"
问题是在我的 build.gradle 中,我使用的 Kotlin 版本是 1.5.0
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.0"
但据我了解,这个版本的 Kotlin 以及 Room 和协程并不能很好地工作。
能够通过将 Kotlin 版本降级到:
来解决问题
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.32"
已解决!稍后谢谢我
在 gradle 中更改为:kapt "androidx.room:room-compiler:$roomVersion" 为:annotationProcessor "androidx.room:room-compiler:$room_version “
这个解决方案对我有部分帮助,但是当我试图在数据库中插入一个日期时,它对我不起作用,所以尝试使用 kapt "androidx.room:room-compiler:$roomVersion" 并更改 room_version到最新稳定版https://developer.android.com/jetpack/androidx/releases/room#groovy
附属物 {
//**
def room_version = '2.4.0'
def activityVersion = '1.4.0'
def lifecycle_version = "2.2.0"
// Room and Lifecycle dependencies
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
//kotlin extensions for coroutine support with room
implementation("androidx.room:room-ktx:$room_version")
//kotlin extension for coroutine support with activities
implementation "androidx.activity:activity-ktx:$activityVersion"
//**
这对我来说是正确的
这是一个版本错误。尝试更新您的房间依赖项。
我改变了我的:implementation "androidx.room:room-runtime:2.2.5"
收件人:implementation "androidx.room:room-runtime:2.4.2"
我正在使用 Room 架构组件来实现持久性。我创建了通用的 DAO 接口以避免样板代码。 Room Pro Tips
但是我的代码没有编译说 "Error:(21, 19) error: Type of the parameter must be a class annotated with @Entity or a collection/array of it." 通用 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();
}
这里有什么我遗漏的吗? 它是这样工作的 here
我最初是按照 Kotlin 中使用的方法,但在 Java 代码中给出了错误。 两个快速更改为我修复了它
- 将 BaseDao 更改为抽象 class
- 向 BaseDao 添加 @Dao 注释
请找到下面的代码,现在可以正常运行了
@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 class(包含@Entity(tableName = "your_table", indices = [Index("your_key")])
).
我相信是你错过了给 T class 的 Entity
注释。比如Reason
class 应该有@Entity
给ReasonDao class。喜欢:
@Dao
public abstract class ReasonDao extends BaseDao<Reason>{}
原因是您将 ReasonDao 类型指定为通用参数,而不是 Reason.
原代码:
@Dao
public abstract class ReasonDao implements BaseDao<ReasonDao> {
...
}
正确代码:
@Dao
public abstract class ReasonDao implements BaseDao<Reason> {
...
}
其中原因是标有@Entity[的类型=31=]注解.
顺便说一句,这已在接受的答案中修复,但未在更改列表中提及:)
What is missing here is the Data class. Generally, @Entity represents objects you want to store,
- Room entity includes fields for each column inside the
- table in the database
@Entity(tableName="something") data class YourData()
对于那些在 dao 中使用 Kotlin 时出现“参数类型必须是用 @Entity 注释的 class 或它的 collection/array”错误的人,您应该尝试使用 @函数上的 JvmSuppressWildcards 注释。例如
@Query("SELECT * FROM materials")
@JvmSuppressWildcards
fun getAllMaterials(): LiveData<List<MaterialModel>>
在 gradle 中更改自:
kapt "androidx.room:room-compiler:$roomVersion"
对此:
annotationProcessor "androidx.room:room-compiler:$room_version"
问题是在我的 build.gradle 中,我使用的 Kotlin 版本是 1.5.0
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.0"
但据我了解,这个版本的 Kotlin 以及 Room 和协程并不能很好地工作。 能够通过将 Kotlin 版本降级到:
来解决问题implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.32"
已解决!稍后谢谢我
在 gradle 中更改为:kapt "androidx.room:room-compiler:$roomVersion" 为:annotationProcessor "androidx.room:room-compiler:$room_version “
这个解决方案对我有部分帮助,但是当我试图在数据库中插入一个日期时,它对我不起作用,所以尝试使用 kapt "androidx.room:room-compiler:$roomVersion" 并更改 room_version到最新稳定版https://developer.android.com/jetpack/androidx/releases/room#groovy 附属物 {
//**
def room_version = '2.4.0'
def activityVersion = '1.4.0'
def lifecycle_version = "2.2.0"
// Room and Lifecycle dependencies
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
//kotlin extensions for coroutine support with room
implementation("androidx.room:room-ktx:$room_version")
//kotlin extension for coroutine support with activities
implementation "androidx.activity:activity-ktx:$activityVersion"
//**
这对我来说是正确的
这是一个版本错误。尝试更新您的房间依赖项。
我改变了我的:implementation "androidx.room:room-runtime:2.2.5"
收件人:implementation "androidx.room:room-runtime:2.4.2"