2

I am quite familiar with the relation among tables, especially when used SQLite. Recently I moved to Room with MVVM clean architecture in mind, I came to a lot of confusion during establishing a relation.

For simplicity, I am fetching the Github repo list and storing in DB using Room.

Here's 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"
}
]
}
]

This are my Entity classes.

@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, "", "", "")
}

And my AppDataBase class

   @Database(entities = [GithubEntity::class, BuiltByEntity::class], version = 1, exportSchema = false)
@TypeConverters(BuiltByConverter::class)
abstract class AppDatabase : RoomDatabase() {

So, I was expecting a kind of output like this

GitHubEntity's rowId (PK) is being used as FK in BuiltByEnity rowFkId(FK)

enter image description here

SO, when I debugged using stetho, Here's the output I got

enter image description here

enter image description here

I am finding difficulty to turn my around with Room. Could you highlight what should be modified and where?

Community
  • 1
  • 1
hemen
  • 1,460
  • 2
  • 16
  • 35

1 Answers1

0

A way to fix is to remove this:

@SerializedName("builtBy")
@Expose var builtByList: List<BuiltByEntity>? = null

from GithubEntity. Insert a GithubEntity item and by its id insert BuiltByEntity.

You see, it is nasty. Notice that there is no native way to insert with relations in room. Read this question and answers for best practices.

Update:

Read this article too: Android Room Database Tips and Tricks

Sina
  • 2,683
  • 1
  • 13
  • 25
  • Hi @Sina, I used to do that when using the SQLite way. I went to that post (highlighted above) thoroughly earlier and kind of expecting that, this behavior might have changed now. I just wanted to confirm !! – hemen Dec 05 '19 at 12:38
  • As I know, it has not changed yet. We all bear the same pain here :) – Sina Dec 05 '19 at 12:53
  • I've just found what was very helpful for me on the subject. I updated my answer. – Sina Dec 05 '19 at 13:28