当数据类具有内部类时,会议室数据库表创建错误

时间:2019-08-26 18:34:33

标签: database kotlin android-room

要求是解析json数据并使用Kotlin将其插入到会议室数据库中。解析已经完成,但是在使用数据类创建表时会出现问题,因为数据类具有内部类。无法使用内部类内部的字段创建表。

无法理解如何使用@TypeConverters

@Entity(tableName = "tbl_newsData")
@TypeConverters(ReposPersistentConverter::class)
data class Article(
    @PrimaryKey(autoGenerate = true) var _id: Long?,
    @ColumnInfo(name = "author") var author: String?,
    @ColumnInfo(name = "title") var title: String?,
    @ColumnInfo(name = "description") var description: String?,
    @ColumnInfo(name = "url") var url: String?,
    @ColumnInfo(name = "urlToImage") var urlToImage: String?,
    @ColumnInfo(name = "publishedAt") var publishedAt: String?,
    @ColumnInfo(name = "content") var content: String?,
    var source: Source?
){
    constructor() : this(null,"", "", "", "", "",
        "", "", "", "",null)
}

class ReposPersistentConverter {
    val gson = Gson()
    // RepoOwner
    @TypeConverter
    fun storeRepoOwnerToString(data: Source): String = gson.toJson(data)

    @TypeConverter
    fun storeStringToRepoOwner(value: String): Source = gson.fromJson(value)
}

@Entity(tableName = "tbl_newsData")
data class Article(
    @PrimaryKey(autoGenerate = true) var _id: Long?,
    @ColumnInfo(name = "author") var author: String?,
    @ColumnInfo(name = "title") var title: String?,
    @ColumnInfo(name = "description") var description: String?,
    @ColumnInfo(name = "url") var url: String?,
    @ColumnInfo(name = "urlToImage") var urlToImage: String?,
    @ColumnInfo(name = "publishedAt") var publishedAt: String?,
    @ColumnInfo(name = "content") var content: String?,
    var source: Source?  //inner class
){
    constructor() : this(null,"", "", "", "", "",
        "", "", "", "",null)
}

////源类

data class Source(
    val id: String,
    val name: String
)

// json

{

    "source": {
        "id": null,
        "name": "Geeksofdoom.com"
    },
    "author": "The Movie God",
    "title": "Hulu Comings and Goings: What’s New and What’s Leaving In September 2019",
    "description": "For those of you who do your entertainment streaming on Hulu, whether it be responsibly in moderation or recklessly in full-on binge watching sessions, there's plenty of TV shows and movies arriving and departing each month to keep track of. The titles set to…",
    "url": "https://www.geeksofdoom.com/2019/08/26/hulu-comings-goings-new-leaving-september-2019",
    "urlToImage": "https://www.geeksofdoom.com/GoD/img/2016/02/hulu.jpg",
    "publishedAt": "2019-08-26T18:00:53Z",
    "content": "For those of you who do your entertainment streaming on Hulu, whether it be responsibly in moderation or recklessly in full-on binge watching sessions, there’s plenty of TV shows and movies arriving and departing each month to keep track of.The titles set to … [+8407 chars]"

}

错误:无法弄清楚如何将该字段保存到数据库中。您可以考虑为其添加类型转换器。     私人com.app.newsapp.dashboard.model.Source来源;

1 个答案:

答案 0 :(得分:1)

@Entity(tableName = "tbl_newsData")
data class Article(
    @PrimaryKey(autoGenerate = true) var _id: Long?,
    @ColumnInfo(name = "author") var author: String?,
    @ColumnInfo(name = "title") var title: String?,
    @ColumnInfo(name = "description") var description: String?,
    @ColumnInfo(name = "url") var url: String?,
    @ColumnInfo(name = "urlToImage") var urlToImage: String?,
    @ColumnInfo(name = "publishedAt") var publishedAt: String?,
    @ColumnInfo(name = "content") var content: String?,
    @TypeConverters(SourceTypeConverter::class)
    @ColumnInfo(name = "source")
    var source: Source?
){
    class SourceTypeConverter {
        @TypeConverter
        fun fromDeliveryExchangeList(source: Source?): String? {
            if (source == null) {
                return null
            }
            val gson = Gson()
            val type = object : TypeToken<Source>() {

            }.type
            return gson.toJson(source, type)
        }

        @TypeConverter
        fun toDeliveryExchangeList(source: String?): Source? {
            if (source == null) {
                return null
            }
            val gson = Gson()
            val type = object : TypeToken<Source>() {

            }.type
            return gson.fromJson(source, type)
        }
    }

    constructor() : this(null,"", "", "",
        "", "", "", "",null)
}

/////////添加转换器

@Database(entities = arrayOf(Article::class), version = 1)
@TypeConverters(Article.SourceTypeConverter::class)
abstract class AppDataBase : RoomDatabase() {
}