Search code examples
androidkotlinandroid-roomkotlin-coroutines

Error: Type of the parameter must be a class annotated with @Entity or a collection/array


I know that some have already posted this topic but after reviewed all the given answer, I can not find anything suit my case. I would be happy if someone can help me resolving my case.

I started getting my build failed as I've updated my Kotlin lib from 1.5.31 to 1.6.0. My Android Room BaseDao class can’t be compiled again. Below my BaseDao class:

interface BaseDao<T> {
    /**
     * Insert an object in the database.
     *
     */
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(obj: T): Long

    /**
     * Insert an array of objects in the database.
     *
     * @param obj the objects to be inserted.
     */
    @Insert
    suspend fun insert(vararg obj: T): LongArray

    /**
     * Update an object from the database.
     *
     * @param obj the object to be updated
     */
    @Update(onConflict = OnConflictStrategy.REPLACE)
    suspend fun update(obj: T)

    /**
     * Delete an object from the database
     *
     * @param obj the object to be deleted
     */
    @Delete
    suspend fun delete(obj: T)
}

@Transaction
suspend inline fun <reified T> BaseDao<T>.insertOrUpdate(item: T) {
    if (insert(item) != -1L) return
    update(item)
}

After build:

BaseDao.java:19: 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 insert(T obj, @org.jetbrains.annotations.NotNull()

This is how i call my BaseDao in one of my Dao class:

@Dao
interface CustomDao : BaseDao<CustomEntity> {
   
}

I've tried @JvmSuppressWildcards but it doesn't help me.


Solution

  • I've finally found a solution to update Kotlin from 1.5.31 to 1.6.10 and my room version from 2.3.0 to 2.4.0.

    The error I was getting, comes from a function I wrote in the embedded data class, as soon as I removed it then everything works.

    @Entity(primaryKeys = ["device_id"],)
    data class DetailedDataEntity(
        @ColumnInfo(name = "device_id", index = true)
        var deviceId: String,
    
        @ColumnInfo(name = "unix_timestamp")
        var unixTimestamp: Long,
    
        @Embedded(prefix = "data_")
        var data: DetailedData
    )
    
    
    data class DetailedData( @ColumnInfo(name = "nameFx") val nameFx: String) {
        companion object {
            @Ignore
            fun fromData(detailedData: Protobuf.Data): DetailedData {
                return DetailedData(nameFx = detailedData.nameFx)
            }
        }
    }
    
    
        companion object {
             @Ignore
             fun fromData(detailedData: Protobuf.Data): DetailedData {
                 return DetailedData(nameFx = detailedData.nameFx)
             }
        }
    

    I've removed this companion object and then when I build the project, it's successfully passed. honestly, I do not know why this but I think it must have something to do with Google protobuf message associated with my embedded class.