31

I decided to use kotlin with Room library and I really faced a lot of problems and tired from reading reference and finding a solution My Data Class:

@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
)

My Dao class :

@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 Class:

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

QueuedSong is working great but the problem in historySong is:

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()

and :

FAILURE: Build failed with an exception.

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

I really can't find a solution.. Thanks in advance

1
  • For starters, you're trying to insert and delete HistorySongDao objects instead of HistorySong objects. See if fixing that helps with some of these errors. Commented Feb 1, 2018 at 18:23

5 Answers 5

91

I solved it by changing my @Database's argument (RoomDatabase class), I didn't realize and put my DAO where should be my Entity.

In my case:

@Database(entities = [WordDao::class],version = 1)

changed to:

@Database(entities = [WordEntity::class],version = 1)

So, when somebody gets this error, maybe should to check not only where the entity is declared but too where it is used.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks from 2024 back to you in 2019! I spent an hour on this and looked and looked and looked. I have several entities and they all worked, but not my latest addition. Thanks to you I immediately spotted I'd inadvertently added "Dao" to the end of the new class name. So easy to miss. Thank you!
13

As it said in the first comment, you are trying to insert and delete HistorySongDao objects instead of HistorySong, your code will become:

@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)
}

Comments

11

Update Database Class to this

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

I change HistorySongDao.class to HistorySong.class

Comments

9

You have mistaken in your Database class with entities. Maybe you are calling Dao Interface rather then Entity class.

2 Comments

Can you tell the class names in your answer. It will be more helpful for understanding.
Open your Database class right here.. @Database (entities ={Entity.class}, version=1,exportedSchema =false) > look at the the entities={ } part here maybe you are calling your Dao interface or something else.
3

By me it was a different problem: [ instead of {

I put :

@Database(entities = [Sensor.class, Meter.class],version = 2)

instead of:

@Database(entities ={Sensor.class, Meter.class}, version = 2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.