如何在Android Room中存储与多个实体相关的实体?

问题描述 投票:0回答:1

我有3个实体。 BOOKUSEROBSERVATION

这是BOOK。只是数据

data class Book(
    val id: Int,
    val title: String,
    val author: String,
    val pages: Int?,
    val editorial: String?,
    val category: String?,
    val description: String?,
    val img: String?
)

这是USER。只是数据。

data class User(
    val username: String,
    val email : String,
    val firstName: String,
    val lastName: String,
    val photoUrl: String?,
    val categories : List<Category>
)

现在,是一个OBSERVATION,其中包含一个USER和一个BOOK

data class Observation(
    val id: Int,
    val user: User,
    val book: Book,
    val page: Int,
    val description: String
)

这意味着从1个表(OBSERVATION)到2个表(BOOKUSER)之间存在关系。

我发现的所有示例都与1与n,n与1,n与m的关系有关。但是在这种情况下,OBSERVATION与2个关系1到1有关。

存储和恢复这种ENTITIES的方式是什么?

android android-room androidx android-livedata
1个回答
1
投票

1.-首先,您需要为User表创建一个id列。 UserBook表中的id将设置为PRIMARY KEY(该表中每一行的唯一标识符)。然后,在观察表中,将用户和书的列类型更改为整数。您无需将对象存储在此处,只需将每个表中的id都存储在那里,这两列将设置为FOREIGN KEY。您已经创建了关系数据库。

2.-要使用Room执行此操作,您可以执行以下操作:

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.ForeignKey.CASCADE
import androidx.room.PrimaryKey

@Entity(tableName = "Book")
data class Book(
        @ColumnInfo(name = "id") @PrimaryKey(autoGenerate = true) val id: Int,
        @ColumnInfo(name = "title") val title: String,
        @ColumnInfo(name = "author") val author: String,
        @ColumnInfo(name = "pages") val pages: Int?,
        @ColumnInfo(name = "editorial") val editorial: String?,
        @ColumnInfo(name = "category") val category: String?,
        @ColumnInfo(name = "description") val description: String?,
        @ColumnInfo(name = "img") val img: String?
)

@Entity(tableName = "User")
data class User(
        @ColumnInfo(name = "id") @PrimaryKey(autoGenerate = true) val id: Int,
        @ColumnInfo(name = "username") val username: String,
        @ColumnInfo(name = "email") val email : String,
        @ColumnInfo(name = "firstName") val firstName: String,
        @ColumnInfo(name = "lastName") val lastName: String,
        @ColumnInfo(name = "photoUrl") val photoUrl: String?,
        @ColumnInfo(name = "categories") val categories : List<Category>
)

@Entity(
        tableName = "list_items",
        foreignKeys = [ForeignKey(
                entity = User::class,
                parentColumns = ["id"],
                childColumns = ["user_id"],
                onDelete = CASCADE),
                       ForeignKey(
                    entity = Book::class,
                    parentColumns = ["id"],
                    childColumns = ["book_id"],
                    onDelete = CASCADE)])
data class Observation(
        @ColumnInfo(name = "id") @PrimaryKey(autoGenerate = true) val id: Int,
        @ColumnInfo(name = "user_id") val userId: Int,
        @ColumnInfo(name = "book_id") val bookId: Int,
        @ColumnInfo(name = "page") val page: Int,
        @ColumnInfo(name = "description") val description: String
)

希望对您有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.