Android RoomKotlin抛出删除查询错误

mw3dktmi  于 2023-06-04  发布在  Android
关注(0)|答案(7)|浏览(453)

我正在尝试使用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);

反刀:

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
}

我以前没有遇到过任何问题,突然之间我得到了这些错误,我不知道是什么导致了它们。
谢谢!

6ljaweal

6ljaweal1#

更新房间版本:2.4.3Kotlin:1.7.20

这解决了问题。

nzk0hqpo

nzk0hqpo2#

设置Kotlin版本为1.5.21或1.5.31
Kotlin1.6.0无法在ROOM @QUERY中使用suspend
选择以下解决方案之一
1.打开你的root build.gradle并添加这个
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21'
1.打开你的模块build.gradle并将2.4.0-alpha 03中的room-version更改为2.4.0-beta 01
def roomVersion = "2.4.0-alpha03"

n6lpvg4x

n6lpvg4x3#

我解决这个问题的方法如下
我把Kotlin版本设置为1.6.10,把Room设置为2.4.2

6za6bjd0

6za6bjd04#

使用@Delete注解,你必须定义一个返回数据类型:
DELETE查询方法必须返回void或int(删除的行数)。
所以这应该是:

@Delete
suspend fun deleteProject(project: Project): Integer

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

7uzetpgm

7uzetpgm5#

检查Answer应该对您有帮助。

yh2wf1be

yh2wf1be6#

作为更新,您现在可以使用Kotlin 1.7.10将您的房间版本更改为Room 2.4.3,所有问题都将得到解决

s3fp2yjn

s3fp2yjn7#

在尝试其他更有效的方法之前,您可以尝试删除suspend。我也遇到过类似的问题。并建议由高级尝试删除它和该项目进行顺利,直到日期

相关问题