首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:实体类必须使用@Entity进行注释

错误:实体类必须使用@Entity进行注释
EN

Stack Overflow用户
提问于 2018-02-02 01:52:32
回答 5查看 18.7K关注 0票数 16

我决定将kotlin与Room库一起使用,我真的面临着很多问题,我已经厌倦了阅读参考资料和寻找解决方案我的数据类:

代码语言:javascript
运行
复制
@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类:

代码语言:javascript
运行
复制
@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)
}

数据库类:

代码语言:javascript
运行
复制
@Database(entities = arrayOf(QueuedSong::class, HistorySongDao::class), version = 2)
abstract class AppDataBase : RoomDatabase() {
    abstract fun queuedSongDao(): QueuedSongDao
    abstract fun historySongDao(): HistorySongDao
}

QueuedSong运行得很好,但historySong中的问题是:

代码语言:javascript
运行
复制
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()

和:

代码语言:javascript
运行
复制
FAILURE: Build failed with an exception.

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

我真的找不到解决办法..提前感谢

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-02-02 03:46:04

正如它在第一条评论中所说的,您正在尝试插入和删除HistorySongDao对象,而不是HistorySong,您的代码将变成:

代码语言:javascript
运行
复制
@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)
}
票数 11
EN

Stack Overflow用户

发布于 2019-02-16 03:14:37

我通过更改@数据库的参数(RoomDatabase类)解决了这个问题,我没有意识到并将我的DAO放在了应该是我的实体的地方。

在我的例子中:

代码语言:javascript
运行
复制
@Database(entities = [WordDao::class],version = 1)

更改为:

代码语言:javascript
运行
复制
@Database(entities = [WordEntity::class],version = 1)

因此,当有人收到这个错误时,可能不仅应该检查实体被声明的位置,还应该检查它被使用的位置。

票数 44
EN

Stack Overflow用户

发布于 2018-09-26 17:08:51

将数据库类更新为此

代码语言:javascript
运行
复制
@Database(entities = arrayOf(QueuedSong::class, HistorySong::class), version = 2)
abstract class AppDataBase : RoomDatabase() {
abstract fun queuedSongDao(): QueuedSongDao
abstract fun historySongDao(): HistorySongDao
}

我将HistorySongDao.class更改为HistorySong.class

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48568906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档