代码之家  ›  专栏  ›  技术社区  ›  noloman amram99

Android Room:创建一对一关系实体

  •  0
  • noloman amram99  · 技术社区  · 4 年前

    我正在用Room和三个表(作业、标签、类别)创建一个小项目。

    基本上:

    • 作业属于(或只有)1个类别,但作业可以有0..N个标签。
    • 一个类别有0..N个作业。
    • 一个标签有0..N个作业。

    我在尝试对所有数据进行建模时遇到了一些困难,尤其是在实体之间的关系方面。更具体地说,我有:

    fun JobListing.toJobEntityList(): List<JobEntity> {
        val jobEntityList = mutableListOf<JobEntity>()
        this.jobs.take(50).forEach { job: Job ->
            jobEntityList.add(
                JobEntity(
                    jobId = job.id,
                    title = job.title,
                    url = job.url,
                    companyName = job.companyName,
                    jobType = job.jobType,
                    publicationDate = job.publicationDate,
                    relocation = job.candidateRequiredLocation,
                    salary = job.salary
                )
            )
        }
        return jobEntityList
    }
    

    当我从网络获取数据时,会调用此扩展函数,因此我可以将其转换为实体并将其存储在我的数据库中。 我基本上是在创造一个 JobEntity ,但作业应关联1个类别和0..N个标签。问题是,我不知道如何添加与作业及其类别和标签之间的关系相关的数据。

    这是我的 工作实体 类别:

    @Entity(
        tableName = "Jobs",
        indices = [
            Index("id", unique = true)
        ]
    )
    data class JobEntity(
        @PrimaryKey @ColumnInfo(name = "id") val jobId: Int,
        @ColumnInfo(name = "title") val title: String,
        @ColumnInfo(name = "url") val url: String,
        @ColumnInfo(name = "company_name") val companyName: String,
        @ColumnInfo(name = "job_type") val jobType: String,
        @ColumnInfo(name = "publication_date") val publicationDate: String,
        @ColumnInfo(name = "candidate_required_location") val relocation: String,
        @ColumnInfo(name = "salary") val salary: String
    )
    

    提前感谢!

    1 回复  |  直到 4 年前
        1
  •  1
  •   Rustam Samandarov    4 年前

    嗨,你应该这样建模你的数据库:

    1. 职位_类别
    2. 标签
    3. 作业(添加category_id字段,并向category表添加1个外键引用)
    4. Job_Tags(添加tag_id、Job_id字段,并添加2个引用类别和作业表的外键)

    enter image description here

    @Entity(tableName = "Category")
    data class CategoryEntity(
        @PrimaryKey @ColumnInfo(name = "id") val id: Int,
    )
    
    @Entity(tableName = "Tags")
    data class TagEntity(
        @PrimaryKey @ColumnInfo(name = "id") val id: Int,
    )
    
    @Entity(
        tableName = "Jobs",
        foreignKeys = [ForeignKey(entity = CategoryEntity::class, parentColumns = ["id"], childColumns = ["categoryid"])]
    )
    data class JobEntity(
        @PrimaryKey @ColumnInfo(name = "id") val id: Int,
        @ColumnInfo(name = "categoryid", index = true) val categoryid: Int,
        // ... other fields
    )
    @Entity(
        tableName = "JobTags",
        foreignKeys = [
            ForeignKey(entity = TagEntity::class, parentColumns = ["id"], childColumns = ["tagId"]),
            ForeignKey(entity = JobEntity::class, parentColumns = ["id"], childColumns = ["jobId"]),
        ]
    )
    data class JobTagEntity(
        @PrimaryKey @ColumnInfo(name = "id") val id: Int,
        @ColumnInfo(name = "tagId", index = true) val tagId: Int,
        @ColumnInfo(name = "jobId", index = true) val jobId: Int,
    )