Android Room Kotlin抛出删除查询错误。

14

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

ProjectDao:

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);

计数器DAO:

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)

然而,我的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
}

我之前没有遇到过这种问题,突然间出现了这些错误,我不知道是什么原因导致的。

谢谢!


你是否依赖于 room-ktx?请在你的 build.gradle 文件中包含它。 - ianhanniballake
请查看此答案以获取帮助:链接 - Marawan Mamdouh
9个回答

16

更新 房间版本:2.4.3 Kotlin: 1.7.20

这解决了问题。


9

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

  1. 打开根目录下的build.gradle文件并添加如下内容:
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21'
  2. 打开模块下的build.gradle文件,并将room-version更改为2.4.0-alpha03到2.4.0-beta01中的一个
    def roomVersion = "2.4.0-alpha03"

5

我按照以下方式解决了这个问题

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


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

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

因此应该这样写:
@Delete
suspend fun deleteProject(project: Project): Integer

这将在数据库中存在project时返回1,当project不存在时返回0

如果方法返回值不明确,那么它将编译为 Unit,它等同于 Void 吗? - cutiko
这实际上是一个“接口”定义 - 错误消息使您的论点无效。如果是这种情况,它不会要求定义返回类型“Integer”或“Void” - 从中生成实际方法。 - Martin Zeitler

2
这是一个特定房间和Kotlin组合的问题。 在撰写这篇帖子时,您可以将其更正为:
def room_version = 2.6.0-beta01

另外,如果你需要升级你的Gradle插件到:
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21'

1

请查看这个答案,它应该能帮到你。


1
作为一个更新,你现在可以使用 Kotlin 1.7.10 将你的 Room 版本更改为 Room 2.4.3 ,这样就可以解决所有问题了。

1
这个错误发生在你在@dao实体中使用了suspend关键字时,从@dao类中移除suspend关键字即可解决。
@Dao
interface ProjectDao {
    @Query("SELECT * FROM table_projects")
    fun getAll(): List<Project>

    @Insert
    fun insertProject(project: Project): Long

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

    @Delete
    fun deleteProject(project: Project)

    @Query("DELETE FROM table_projects")
    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
}

默认情况下,最新版本的房间实体的所有方法都已经是异步的。
或者将房间版本更新为2.4.0。

0

在尝试其他更有效的方法之前,您可以尝试删除 suspend。我曾遇到过类似的问题,一位资深人士建议我尝试将其删除,从那时起,项目至今一直顺利进行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接