Search code examples
androidkotlinandroid-roomdao

Not sure how to handle insert method's return type. public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull()


I am complete new to kotlin, and trying to build a notes app using MVVM architecture. But somewhere I go wrong and end up with this error. My kotlin version is 1.5.31

NoteDao.java:11: error: Not sure how to handle insert method's return type.
    public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull()

NoteDao.kt

@Dao
interface NoteDao {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(note:Note): Note

    @Delete
    suspend fun delete(note: Note): Note

    @Query("SELECT * FROM notes_table ORDER BY primary_key ASC")
    fun getAllNotes(): LiveData<List<Note>>
}

Note.kt

@Entity(tableName = "notes_table")
class Note(@ColumnInfo(name = "note_text", typeAffinity = TEXT) val text: String){
    @PrimaryKey(autoGenerate = true) @ColumnInfo(name="primary_key", typeAffinity = INTEGER) var id = 0
}

I am not sure how to handle this.


Solution

  • Try doing this:

    @Dao
    abstract class BaseDao<T : BaseEntity>(private val tableName: String) {
        @Insert(onConflict = OnConflictStrategy.REPLACE)
        abstract suspend fun insert(entity: T): Long
    
        @Update
        abstract suspend fun update(entity: T)
    
        @Delete
        abstract suspend fun delete(entity: T)
    }
    
    abstract class BaseEntity {
        abstract val id: Long
    }