kotlin 房间DB三表关系

zc0qhyus  于 2023-11-21  发布在  Kotlin
关注(0)|答案(1)|浏览(253)
  • 我试图在房间数据库中使用@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>
)

ma8fv8wu

ma8fv8wu1#

您可以在student表中添加一个名为subject_id的列信息,然后可以在student和subject表之间创建关系。

相关问题