我对表之间的关系非常熟悉,尤其是在使用SQLite时。 最近,我考虑到MVVM干净的体系结构迁移到Room,在建立关系时遇到了很多困惑。
为简单起见,我获取Github存储库列表并使用Room存储在数据库中。
这是JSON
[
{
"author": "idealvin",
"name": "co",
"avatar": "https://github.com/idealvin.png",
"url": "https://github.com/idealvin/co",
"description": "An elegant and efficient C++ basic library for Linux, Windows and Mac.",
"language": "C++",
"languageColor": "#f34b7d",
"stars": 635,
"forks": 82,
"currentPeriodStars": 130,
"builtBy": [
{
"username": "idealvin",
"href": "https://github.com/idealvin",
"avatar": "https://avatars1.githubusercontent.com/u/12691466"
},
{
"username": "frederick-vs-ja",
"href": "https://github.com/frederick-vs-ja",
"avatar": "https://avatars3.githubusercontent.com/u/23228989"
},
{
"username": "FrankHB",
"href": "https://github.com/FrankHB",
"avatar": "https://avatars0.githubusercontent.com/u/1857647"
}
]
},
{
"author": "alyssaxuu",
"name": "flowy",
"avatar": "https://github.com/alyssaxuu.png",
"url": "https://github.com/alyssaxuu/flowy",
"description": "The minimal javascript library to create flowcharts ✨",
"language": "JavaScript",
"languageColor": "#f1e05a",
"stars": 1841,
"forks": 58,
"currentPeriodStars": 459,
"builtBy": [
{
"username": "alyssaxuu",
"href": "https://github.com/alyssaxuu",
"avatar": "https://avatars3.githubusercontent.com/u/7581348"
},
{
"username": "artmsilva",
"href": "https://github.com/artmsilva",
"avatar": "https://avatars3.githubusercontent.com/u/347490"
}
]
}
]
这是我的Entity类。
@Entity
data class GithubEntity(
// Ignore this field during Serialization and De-Serialization
// PK (AUTO INC)
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "rowId")
@Expose(serialize = false, deserialize = false) var rowId: Int,
@ColumnInfo(name = "author")
@SerializedName("author")
@Expose var author: String? = null,
@ColumnInfo(name = "name")
@SerializedName("name")
@Expose var name: String? = null,
@ColumnInfo(name = "avatar")
@SerializedName("avatar")
@Expose var avatar: String? = null,
@ColumnInfo(name = "url")
@SerializedName("url")
@Expose var url: String? = null,
@ColumnInfo(name = "description")
@SerializedName("description")
@Expose var description: String? = null,
@ColumnInfo(name = "language")
@SerializedName("language")
@Expose var language: String? = null,
@ColumnInfo(name = "languageColor")
@SerializedName("languageColor")
@Expose var languageColor: String? = null,
@ColumnInfo(name = "stars")
@SerializedName("stars")
@Expose var stars: Int? = null,
@ColumnInfo(name = "forks")
@SerializedName("forks")
@Expose var forks: Int? = null,
@ColumnInfo(name = "currentPeriodStars")
@SerializedName("currentPeriodStars")
@Expose var currentPeriodStars: Int? = null,
@SerializedName("builtBy")
@Expose var builtByList: List<BuiltByEntity>? = null
) {
constructor() : this(
0, "", "", "", "",
"", "", "", 0, 0, 0, null
)
}
@Entity(
foreignKeys = [ForeignKey(
entity = GithubEntity::class,
parentColumns = arrayOf("rowId"),
childColumns = arrayOf("rowFkId"),
onDelete = ForeignKey.CASCADE
)]
)
data class BuiltByEntity(
// PK (AUTO INC)
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "row_Built_Id")
@Expose(serialize = false, deserialize = false) var rowId: Int,
// FOREIGN KEY -> rowId (PK) of GitHubEntity
@ColumnInfo(name = "rowFkId")
@Expose(serialize = false, deserialize = false) var rowFkId: Int,
@ColumnInfo(name = "username")
@SerializedName("username")
@Expose var username: String? = null,
@ColumnInfo(name = "href")
@SerializedName("href")
@Expose var href: String? = null,
@ColumnInfo(name = "avatar")
@SerializedName("avatar")
@Expose var avatar: String? = null
) {
constructor() : this(0, 0, "", "", "")
}
还有我的AppDataBase类
@Database(entities = [GithubEntity::class, BuiltByEntity::class], version = 1, exportSchema = false)
@TypeConverters(BuiltByConverter::class)
abstract class AppDatabase : RoomDatabase() {
所以,我期待这样的输出
GitHubEntity的rowId(PK)在BuiltByEnity rowFkId(FK)中用作FK
所以,当我使用Stetho进行调试时,这是我得到的输出
我在转弯房间方面遇到困难。 您能强调一下哪些地方应该修改吗?
答案 0 :(得分:0)
一种解决方法是删除此内容:
@SerializedName("builtBy")
@Expose var builtByList: List<BuiltByEntity>? = null
来自GithubEntity。插入一个GithubEntity项,并通过其ID插入BuiltByEntity。
你看,这很讨厌。请注意,在房间中没有插入关系的本地方法。阅读this问答,以获取最佳做法。
更新: