DAO错误:-参数类型必须是用@Entity注释的类或其集合/数组

问题描述 投票:0回答:5

我是 Android 开发新手,我正在尝试通过遵循 Android 架构组件来制作一个笔记应用程序,但在运行时,我在 DAO 中遇到错误,如果有人可以提供帮助,我将非常感激。 这是我收到的代码和错误。

DAO:-

'''

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

    @Delete
    suspend fun delete(note : Note)

    @Query("SELECT * FROM Notes_table order by id")
    fun getALL(): LiveData<List<Note>>

    @Query("SELECT * From Notes_table where id= :pos")
    fun getSpecific(pos :Int):Note

}

'''

实体:-

'''

@Entity(tableName = "Notes_table")
data class Note(@ColumnInfo(name="noteText") val text:String) {
    @PrimaryKey(autoGenerate = true) var id:Int =0

}

''' 数据库:-

'''

@Database(entities = [Note::class],version = 1,exportSchema = false)
abstract class NoteDatabase : RoomDatabase() {

    abstract fun getNoteDao():NoteDao

    companion object{
        @Volatile
        private var Instance: NoteDatabase?=null

        fun getDatabase(context :Context):NoteDatabase{

            return Instance ?: synchronized(this){
                val instance=Room.databaseBuilder(context.applicationContext,
                NoteDatabase::class.java,"note_database").build()
                Instance=instance
                instance
            }
        }

    }

}

DAO 的导入:-

import androidx.lifecycle.LiveData
import androidx.room.

实体进口:-

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

数据库导入:-

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

如果需要,我可以提供其余的代码。

android kotlin android-room android-architecture-components data-access-object
5个回答
5
投票

在评论部分的帮助下并通过阅读文档,我发现我的 DAO 出了问题,最终这与我添加到依赖项中的 ROOM 版本有关,我刚刚更新了这些版本,现在它工作正常.


3
投票

今天遇到了同样的问题,通过将

Room
版本从
2.3.0
更新到
2.4.0-beta02
来解决。版本可以在这里找到:https://developer.android.com/jetpack/androidx/releases/room#2.3.0-alpha04


3
投票

更新 Room 版本对我有用:2.4.2

   implementation "androidx.room:room-runtime:2.4.2"
   kapt "androidx.room:room-compiler:2.4.2"
   implementation "androidx.room:room-ktx:2.4.2"
   androidTestImplementation "androidx.room:room-testing:2.4.2"

2
投票

通过将房间版本提高到“2.4.0-beta01”来修复


0
投票

2023 年 10 月

kapt
注释处理工具不适用于我的
suspend
功能。

我做了什么来修复它:

根构建.gradle

buildscript {
    ext.room_version = '2.5.2'
}
plugins {
    id 'com.android.application' version '8.1.2' apply false
    //kotlin gradle plugins
    id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
    id 'com.google.devtools.ksp' version '1.9.0-1.0.13' apply false
}

我注意到kotlin gradle插件版本必须与ksp插件版本匹配,否则它不起作用(ksp版本1.8对我不起作用)。

应用程序/build.gradle

plugins {
    //id 'kotlin-kapt'
    id "com.google.devtools.ksp"
}

dependencies {
    implementation "androidx.room:room-runtime:$room_version"
    ksp "androidx.room:room-compiler:$room_version"
    //DOES NOT WORK
    //kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
}
© www.soinside.com 2019 - 2024. All rights reserved.