我决定将Kotlin与Room库一起使用,我真的遇到了很多问题,在阅读引用资料和找到解决方案时很累
我的资料类别:

@Entity
data class HistorySong(
        @PrimaryKey
        var SongId: Int =0,

        @ColumnInfo(name = "song_name")
        var songName: String="",

        @ColumnInfo(name = "song_artist")
        var songArtist: String="",

        @ColumnInfo(name = "song_link")
        var songLink: String="",

        @ColumnInfo(name = "image_path")
        var songImagePath: String="",

        @ColumnInfo(name="is_favoutire")
        var songisFavourite: Boolean= false
)

我的上课:
@Dao
 interface HistorySongDao {

       @Delete
       fun deleteSong(historySongDao: HistorySongDao)

       @Insert(onConflict = OnConflictStrategy.REPLACE)
        fun insert(vararg historySongDao: HistorySongDao)

       @Query("SELECT * FROM HistorySong")
        fun loadAllSongs(): Array<HistorySong>


       @Query("SELECT * FROM HistorySong WHERE songId = :mId")
        fun findById(mId: Int): HistorySong

       @Query("SELECT * FROM HistorySong WHERE is_favoutire = :getFavourite ")
        fun getFavourite(getFavourite : Boolean) : Array<HistorySong>



       @Update
       fun updateUsers(vararg historySong: HistorySong)
}

数据库类:
@Database(entities = arrayOf(QueuedSong::class, HistorySongDao::class), version = 2)
abstract class AppDataBase : RoomDatabase() {
    abstract fun queuedSongDao(): QueuedSongDao
    abstract fun historySongDao(): HistorySongDao
}

QueuedSong工作得很好,但是historySong中的问题是:
e: F:\SmartStreamer\app\build\tmp\kapt3\stubs\debug\com\pro\smartstreamer\Database\QueuedDatabase\HistorySongDao.java:7: error: Entity class must be annotated with @Entity
public abstract interface HistorySongDao {
                ^
w: F:\SmartStreamer\app\build\tmp\kapt3\stubs\debug\com\pro\smartstreamer\Database\QueuedDatabase\AppDataBase.java:10: warning: Room cannot create an SQLite connection to verify the queries. Query verification will be disabled. Error: [SQLITE_ERROR] SQL error or missing database (near ")": syntax error)
public abstract class AppDataBase extends android.arch.persistence.room.RoomDatabase {
                ^
w: F:\SmartStreamer\app\build\tmp\kapt3\stubs\debug\com\pro\smartstreamer\Database\QueuedDatabase\HistorySong.java:10: warning: There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors.
public final class HistorySong {
             ^
e: F:\SmartStreamer\app\build\tmp\kapt3\stubs\debug\com\pro\smartstreamer\Database\QueuedDatabase\HistorySongDao.java:15: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
    com.pro.smartstreamer.Database.QueuedDatabase.HistorySongDao... historySongDao);
                                                                    ^
e: F:\SmartStreamer\app\build\tmp\kapt3\stubs\debug\com\pro\smartstreamer\Database\QueuedDatabase\HistorySongDao.java:11: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
    com.pro.smartstreamer.Database.QueuedDatabase.HistorySongDao historySongDao);
                                                                 ^
e: F:\SmartStreamer\app\build\tmp\kapt3\stubs\debug\com\pro\smartstreamer\Database\QueuedDatabase\HistorySongDao.java:30: error: com.pro.smartstreamer.Database.QueuedDatabase.HistorySongDao is part of com.pro.smartstreamer.Database.QueuedDatabase.AppDataBase but this entity is not in the database. Maybe you forgot to add com.pro.smartstreamer.Database.QueuedDatabase.HistorySong to the entities section of the @Database?
    public abstract void updateUsers(@org.jetbrains.annotations.NotNull()

和 :
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Compilation error. See log for more details

我真的找不到解决方案。
提前致谢

最佳答案

就像在第一条评论中所说的那样,您试图插入和删除HistorySongDao对象而不是HistorySong,您的代码将变为:

@Dao
 interface HistorySongDao {

       @Delete
       fun deleteSong(vararg historySong: HistorySong)

       @Insert(onConflict = OnConflictStrategy.REPLACE)
        fun insert(vararg historySong: HistorySong)

       @Query("SELECT * FROM HistorySong")
        fun loadAllSongs(): Array<HistorySong>

       @Query("SELECT * FROM HistorySong WHERE songId = :mId")
        fun findById(mId: Int): HistorySong

       @Query("SELECT * FROM HistorySong WHERE is_favoutire = :getFavourite ")
        fun getFavourite(getFavourite : Boolean) : Array<HistorySong>

       @Update
       fun updateUsers(vararg historySong: HistorySong)
}

10-06 13:14