@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
}
}
有两个模型类我必须创建两个表一个父作为Calendar事件,它的智能表作为Attendess我已经为实体编写了AppDatabase但是当我编译时我得到了这个异常
e:错误:实体和Pojos必须具有可用的公共构造函数。您可以拥有一个空构造函数或其参数与字段匹配的构造函数(按名称和类型)。 e:
请建议我我错了什么我是新房间数据库我从来没有使用太多我能够创建单个表而不是儿童父母关系表所以请检查建议我在哪里做错了