- 我试图在房间数据库中使用
@Relation
从三个表中实现关系,但与它混淆。 - DB由学生、科目和成绩表组成。
- 学生表 *:
@Entity(tableName = "student")
data class Student(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "student_id")
var id: Int = 0,
@ColumnInfo(name = "student_name")
var name: String? = null,
@ColumnInfo(name = "student_email")
var email: String? = null,
@ColumnInfo(name = "student_created_date")
var created: Long = System.currentTimeMillis(),
var school_id: Int = -1
)
字符串
- 主题表 *:
@Entity(tableName = "subject")
data class Subject(
@ColumnInfo(name = "subject_id")
var id: Int,
@ColumnInfo(name = "subject_name")
var name: String
)
型
- 标记表 *:
@Entity(tableName = "marks")
data class Marks(
@ColumnInfo(name = "student_id")
var stu_id: Int,
@ColumnInfo(name = "subject_id")
var sub_id: Int,
@ColumnInfo(name = "marks")
var marks: Int,
)
型
我想按科目检索学生及其各自的分数。下面是我定义的类,但我不知道如何用three tables
定义关系:
data class StudentWithMarks(
@Embedded var student: Student,
@Relation(
parentColumn = "student_id",
entityColumn = "student_id"
)
var marks: List<Marks>
)
型
1条答案
按热度按时间ma8fv8wu1#
您可以在student表中添加一个名为subject_id的列信息,然后可以在student和subject表之间创建关系。