ROOM Cannot figure out how to save this field into database

做~自己de王妃 提交于 2020-05-16 03:43:07

问题


I need your help. I try to find the solution without deserialize but I didn't find nothing. It would be nice if you can help me... So I have this following JSON file

"resources": [{
    "id": 441988,
    "name": "TVA",
    "parent": {
      "id": 159
    },
  },
  {
    "id": 441900,
    "name": "Marketing",
    "parent": {
      "id": 166
    },
}]

I used ROOM to create a database, it works but I have one problem, its impossible to save "id of Parent object"

here is my following code :

    @Entity(tableName = "resources")
    class CategoryBean {

    @PrimaryKey
    @ColumnInfo(name = "resource_id")
    @SerializedName("id")
    private var mId: Int = 0

    @ColumnInfo(name = "name")
    @SerializedName("name")
    private var mName: String? = null

    @ColumnInfo(name = "parent")
    @SerializedName("parent")
    private var mParent: ParentBean? = null

    fun getId(): Int {
        return mId
    }

    fun getName(): String? {
        return mName
    }

    fun getParent(): ParentBean? {
        return mParent
    }

    fun setParent(parent: ParentBean) {
        mParent = parent
    }

    fun setId(id: Int) {
        mId = id
    }

    fun setName(name: String) {
        mName = name
    }

Here you can find my ParentBean file

class ParentBean {

    @SerializedName("id")
    private var mId: Int = 0

    fun getId(): Int? {
        return mId
    }

}

I found some solution for a list, but not for sub object.

thank you for your time and your help.

There is my following error :

Cannot figure out how to save this field into database. You can consider adding a type converter for it.

回答1:


Room works on top of a SQLite database, when you annotate a class as @Entity you are creating a table whose columns are defined according to the properties of the class, so you can't store directly a custom type like ParentBean, to do that, you need to annotate mParent property as @Embedded:

@Entity(tableName = "resources")
class CategoryBean constructor(
    @PrimaryKey
    @ColumnInfo(name = "resource_id")
    @SerializedName("id")
    var mId: Int,
    @ColumnInfo(name = "name")
    @SerializedName("name")
    var mName: String?,
    @SerializedName("parent")
    @Embedded
    var mParent: ParentBean?
)

Then you define a column name for the property in ParentBean:

data class ParentBean constructor(@SerializedName("id") @ColumnInfo(name = "parent_id")var mId: Int)

resources table will have the following columns: resource_id, name, parent_id

source: https://developer.android.com/training/data-storage/room/defining-data

Note: You don't need to manually define getter and setters in Kotlin.




回答2:


Your problem is your JSON data is not valid.

"resources": [{
  "id": 441988,
  "name": "TVA",
  "parent": {
    "id": 159
  },            //This Line has a comma without another field following it
},
{
  "id": 441900,
  "name": "Marketing",
  "parent": {
    "id": 166
  },           //This Line has a comma without another field following it
}]

Also the entire thing should be surrounded by another set of curly braces (but I am assuming that was left out due to copy & paste).

A good tool to test out your JSON data and your POJOs is this site: http://www.jsonschema2pojo.org

Hope this helps :)



来源:https://stackoverflow.com/questions/55538271/room-cannot-figure-out-how-to-save-this-field-into-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!