0
@Entity(tableName = AppConstant.Companion.CALENDAR_EVENT_TABLE_NAME)
class CalendarEvent : Serializable {
    @PrimaryKey(autoGenerate = true)
    var id: Int? = null
    @ColumnInfo(name = "calendarId")
    var calendarId: Int? = null
    @ColumnInfo(name = "title")
    var title: String? = null
    @ColumnInfo(name = "organizer")
    var organizer: String? = null
    @ColumnInfo(name = "location")
    var location: String? = null
    @ColumnInfo(name = "description")
    var description: String? = null
    @ColumnInfo(name = "startDate")
    var startDate: String? = null
    @ColumnInfo(name = "endDate")
    var endDate: String? = null
    @Embedded
    var attendees: List<Attendees>? = null

    constructor(id: Int,calendarId: Int, title: String, organizer: String, location: String, description: String,startDate: String,endDate: String,attendees: List<Attendees>) {
        this.id = id
        this.calendarId=calendarId
        this.title = title
        this.organizer = organizer
        this.location = location
        this.description = description
        this.startDate=startDate
        this.endDate=endDate
        this.attendees=attendees
    }


    companion object {
        private const val serialVersionUID = -3245196282912380133L
        val TAG = CalendarEvent::class.java.name
    }
}


@Entity(tableName = AppConstant.Companion.ATTENDEES_TABLE_NAME)
class Attendees: Serializable
{
    @PrimaryKey(autoGenerate = true)
    var id: Int? = null
    @ColumnInfo(name = "name")
    var name: String= ""
    @ColumnInfo(name = "email")
    var email: String= ""
    @ColumnInfo(name = "status")
    var status: Int? = null
      constructor(id: Int,name: String,email: String,status: Int)
    {
        this.id=id
        this.name=name
        this.email=email
        this.status=status
    }
    companion object {
        private const val serialVersionUID = -3245196282912380133L
        val TAG = Attendees::class.java.name
    }
}



@Database(entities = arrayOf(Contact::class, CalendarEvent::class,Attendees::class), version = AppConstant.Companion.DATA_BASE_VERSION)

abstract class AppDatabase : RoomDatabase() {

    abstract fun contactDao(): ContactDAO

    companion object {

        var INSTANCE: AppDatabase? = null
        fun getAppDatabase(context: Context): AppDatabase {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, AppConstant.DATABASE_NAME)
                        // allow queries on the main thread.
                        .addMigrations()
                        // Don't do tfallbackToDestructiveMigrationhis on a real app! See PersistenceBasicSample for an example.
                        .build()
            }
            return INSTANCE!!
        }
    }

    fun destroyInstance() {
        INSTANCE = null
    }

}

there are two model class i have to create two table one parent as Calendar Event and its chile table as Attendess i have written AppDatabase for entities but when i compile then i am getting this exception

e: error: 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). e:

please suggest me what i am ding wrong i am new in room databse i never use much i am able to create single table but not child parent relationship table so please check suggest where am doing mistake

Mhanaz Syed
  • 229
  • 1
  • 5
  • 18

0 Answers0