Search code examples
kotlinentityandroid-room

Entity class do not work in room database


following error appears:

Entity class must be annotated with @Entity - androidx.room.Entityerror: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - androidx.room.Entityerror: An entity must have at least 1 field annotated with @PrimaryKey - androidx.room.Entityerror: [SQLITE_ERROR] SQL error or missing database (near ")": syntax error) - androidx.room.EntityH:\Apps2021\app\build\tmp\kapt3\stubs\debug\de\tetzisoft\danza\data\DAO.java:17: error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: geburtstag)

Entity looks like this

@Entity(tableName = "geburtstag")
data class Bday(
    @PrimaryKey(autoGenerate = true)
    var id : Int,
    @ColumnInfo(name="Name")
    var name : String,
    @ColumnInfo(name="Birthday")
    var birth : String
)

Solution

  • The issue would appear to be that you do not have the Bday class defined in the entities in the class that is annotated with @Database.

    e.g. you appear to have :-

    @Database(entities = [],version = 1)
    abstract class TheDatabase: RoomDatabase() {
        abstract fun getDao(): Dao
    }
    

    This produces the results you have show e.g.

        E:\AndroidStudioApps\SO67560510Geburtstag\app\build\tmp\kapt3\stubs\debug\a\a\so67560510geburtstag\TheDatabase.java:7: error: @Database annotation must specify list of entities
    public abstract class TheDatabase extends androidx.room.RoomDatabase {
                    ^error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: geburtstag) - a.a.so67560510geburtstag.Dao.getAll()error: Not sure how to convert a Cursor to this method's return type (java.util.List<a.a.so67560510geburtstag.Bday>). - a.a.so67560510geburtstag.Dao.getAll()error: a.a.so67560510geburtstag.Dao is part of a.a.so67560510geburtstag.TheDatabase but this entity is not in the database. Maybe you forgot to add a.a.so67560510geburtstag.Bday to the entities section of the @Database? - a.a.so67560510geburtstag.Dao.insert(a.a.so67560510geburtstag.Bday)
    

    Whilst :-

    @Database(entities = [Bday::class],version = 1)
    abstract class TheDatabase: RoomDatabase() {
        abstract fun getDao(): Dao
    }
    

    compiles successfully.

    • note the change from entities=[] to entities = [Bday::class]