JamieRhys
JamieRhys

Reputation: 344

Android Room Kotlin throws Delete Query error

I'm trying to use Android Room 2.3.0 and I'm currently getting the compile errors below:

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

CounterDao:

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)

However, my ProjectDao.kt file has the following:

@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
}

I've not had any issue with this previously, and all of a sudden I'm getting these errors and I've no idea what could be causing them.

Thanks!

Upvotes: 17

Views: 13491

Answers (9)

jenos kon
jenos kon

Reputation: 564

I had the same problem and I found two solutions and both of them work for me :

First solution :

  • use room_version = "2.6.1"

  • use compileSdk = 34 and targetSdk = 34

Second solution

change kotlin version

implementation 'androidx.core:core-ktx:1.7.0'

it works with the room version

room_version = "2.4.3"

Upvotes: 0

Rishu Sharma
Rishu Sharma

Reputation: 57

It was a problem with the specific room + kotlin combination. As of writing this post you can correct it to:

def room_version = 2.6.0-beta01

also, if you should upgrade your gradle plugin to:

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21'

Upvotes: 3

Ronik Limbani
Ronik Limbani

Reputation: 917

This error occurs when you use suspend keyword in the room @dao entity, remove suspend keyword from @dao class

@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
}

By default, all methods of room entity are already asynchronous in the latest room version.

or update the room version to 2.4.0

Upvotes: 2

arnauth akb
arnauth akb

Reputation: 41

You can try removing suspend before trying other more efficient methods. I came across a similar problem. and was suggested by a senior to try to remove it and the project is going well till date

Upvotes: 0

rungene
rungene

Reputation: 241

Update room version: 2.4.3 Kotlin: 1.7.20

This solves the problem.

Upvotes: 19

Payam Monsef
Payam Monsef

Reputation: 336

As an update you can now change your room version to Room 2.4.3 with Kotlin 1.7.10 and every thing will be fixed

Upvotes: 1

Desilio Neto
Desilio Neto

Reputation: 560

I solved this problem as follows

I set Kotlin version to 1.6.10 than Room to 2.4.2

Upvotes: 5

yan sam
yan sam

Reputation: 427

Set kotlin version is 1.5.21 or 1.5.31
Kotlin 1.6.0 cannot use suspend in ROOM @QUERY
Choose one of the following solutions

  1. open your root build.gradle and add this
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21'
  2. open your module build.gradle and change room-version one of 2.4.0-alpha03 until 2.4.0-beta01
    def roomVersion = "2.4.0-alpha03"

Upvotes: 11

Martin Zeitler
Martin Zeitler

Reputation: 76699

With the @Delete annotation, you have to define a return data-type:

DELETE query methods must either return void or int (the number of deleted rows).

So this should rather be:

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

This would return 1 on success and 0 when the project didn't exist in database.

Upvotes: 2

Related Questions