错误:实体类必须用@Entity注解
Posted
技术标签:
【中文标题】错误:实体类必须用@Entity注解【英文标题】:error: Entity class must be annotated with @Entity 【发布时间】:2018-07-12 03:23:48 【问题描述】:我决定将 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
对象。看看修复是否有助于解决其中一些错误。
【参考方案1】:
我通过更改 @Database 的参数(RoomDatabase 类)解决了这个问题,但我没有意识到并将我的 DAO 放在应该是我的实体的位置。
就我而言:
@Database(entities = [WordDao::class],version = 1)
改为:
@Database(entities = [WordEntity::class],version = 1)
所以,当有人收到这个错误时,也许不仅要检查实体的声明位置,还要检查它的使用位置。
【讨论】:
成功了。我真的很新来使用房间。浪费了很多时间。 @felipe 你是个传奇,谢谢你为我指明了正确的方向!!【参考方案2】:正如第一条评论中所说,您尝试插入和删除 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)
【讨论】:
【参考方案3】:将数据库类更新为此
@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
【讨论】:
【参考方案4】:你在你的数据库类中误用了实体。 也许你调用的是 Dao Interface 而不是 Entity 类。
【讨论】:
你能说出答案中的类名吗?对理解会更有帮助。 在此处打开您的数据库类.. @Database (entities =Entity.class, version=1,exportedSchema =false) > 看看这里的实体= 部分,也许您正在调用你的 Dao 界面或其他东西。【参考方案5】:对我来说这是一个不同的问题:[ 而不是
我说:
@Database(entities = [Sensor.class, Meter.class],version = 2)
代替:
@Database(entities =Sensor.class, Meter.class, version = 2)
【讨论】:
以上是关于错误:实体类必须用@Entity注解的主要内容,如果未能解决你的问题,请参考以下文章