Android Room Kotlin 抛出删除查询错误

Jam*_*hys 15 android kotlin android-room

我正在尝试使用 Android Room 2.3.0,目前遇到以下编译错误:

项目道:

error: Not sure how to handle query method's return type (java.lang.Object). DELETE query methods must either return void or int (the number of deleted rows).
    public abstract java.lang.Object deleteAllProjects(@org.jetbrains.annotations.NotNull()
error: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
    kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);
error: Unused parameter: continuation
    public abstract java.lang.Object deleteAllProjects(@org.jetbrains.annotations.NotNull()
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
    kotlin.coroutines.Continuation<? super java.lang.Long> continuation);
error: Not sure how to handle insert method's return type.
    public abstract java.lang.Object insertProject(@org.jetbrains.annotations.NotNull()
error: Not sure how to handle delete method's return type. Currently the supported return types are void, int or Int.
    public abstract java.lang.Object deleteProject(@org.jetbrains.annotations.NotNull()
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
    kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);
Run Code Online (Sandbox Code Playgroud)

计数器道:

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)
Run Code Online (Sandbox Code Playgroud)

但是,我的 ProjectDao.kt 文件具有以下内容:

@Dao
interface ProjectDao {
    @Query("SELECT * FROM table_projects")
    fun getAll(): List<Project>

    @Insert
    suspend fun insertProject(project: Project): Long

    @Insert
    fun insertProjects(projects: List<Project>)

    @Delete
    suspend fun deleteProject(project: Project)

    @Query("DELETE FROM table_projects")
    suspend fun deleteAllProjects()

    @Transaction
    @Query("SELECT * FROM table_projects")
    fun getAllProjectsWithCounters(): List<ProjectWithCounters>

    @Transaction
    @Query("SELECT * FROM table_projects WHERE id_project=:projectID")
    fun getProjectWithCounters(projectID: Long): ProjectWithCounters
}
Run Code Online (Sandbox Code Playgroud)

我以前没有遇到过任何问题,突然间我收到了这些错误,而且我不知道是什么原因造成的。

谢谢!

小智 18

更新 房间版本:2.4.3 Kotlin:1.7.20

这解决了问题。


yan*_*sam 9

设置 kotlin 版本为 1.5.21 或 1.5.31
Kotlin 1.6.0 无法suspend在 ROOM 中使用@QUERY
选择以下解决方案之一

  1. 打开你的根 build.gradle 并添加它
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21'
  2. 打开模块 build.gradle 并将room-version 1 更改为 2.4.0-alpha03 直到 2.4.0-beta01
    def roomVersion = "2.4.0-alpha03"


Des*_*eto 5

我解决了这个问题如下

我将 Kotlin 版本设置为1.6.10,将 Room 设置为2.4.2


Mar*_*ler 2

使用@Delete注释,您必须定义返回数据类型:

DELETE 查询方法必须返回 void 或 int (已删除的行数)。

所以这应该是:

@Delete
suspend fun deleteProject(project: Project): Integer
Run Code Online (Sandbox Code Playgroud)

1当成功并且数据库中不存在0时,这将返回。project