Reputation: 4578
I'm learning kotlin with room, and it's pretty easy to insert primitive types in db. Here's how my student data class is declared.
@Entity(tableName = "student")
data class Student(@PrimaryKey(autoGenerate = true) var id: Long = 0,
@ColumnInfo(name="studentID") var studentID: Long = 0,
@ColumnInfo(name="schoolID") var schoolID: Long = 0,
@ColumnInfo(name="guardianID") var guardianID: String = "",
@ColumnInfo(name="matrixNumber") var matrixNumber: String = "",
@ColumnInfo(name="name") var name: String = "",
@ColumnInfo(name="homework") var homework: ArrayList<Homework>,
@ColumnInfo(name="attendance") var attendance: ArrayList<Attendance>
) {}
Homework
and Attendance
are both another data class which holds various primitive types; here's my Homework
data class
@Entity(tableName = "homework")
data class Homework(@PrimaryKey(autoGenerate = true) var id: Long = 0,
@ColumnInfo(name="subject") var subject: String,
@ColumnInfo(name="teacher") var teacher: String,
@ColumnInfo(name="submissionDate") var submissionDate: String = "",
) {
}
I will receive this compilation error on both homework and attendance
Cannot figure out how to save this field into database. You can consider adding a type converter for it.
All the data above will be retrieved using retrofit, if that matters. I read somewhere that I need to use TypedConverter is it? But I'm not really clear on that yet.
Btw I'm coming from using Java and SQLiteHelper, so it wasn't a problem for me using POJO with ArrayList Homework and Attendance in class Student previously, so I was expecting the same with Room.. it seems I'm missing something here.
Upvotes: 0
Views: 684
Reputation: 7279
Room does not support references to other complex objects. There's an article on the reasoning about this decision.
Key takeaway: Room disallows object references between entity classes. Instead, you must explicitly request the data that your app needs.
All you can do with room is defining a @ForeignKey
constraint, which is documented here.
Upvotes: 1