将 POJO 添加到 Room 数据库中 table

Adding a POJO into a Room database table

我有一个 POJO class OfflineDataRequestInfo,我想将其插入数据库 table 作为 OfflineData 的一部分,这是我的实体。但我得到一个错误

请问我该如何解决这个问题

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it. private final com.i6systems.offlineservicelibrary.offlineDatabase.OfflineDataRequestInfo requestInfoJsonX = null;

这是我的代码

data class OfflineDataRequestInfo (
    val status: String,
    val userId: String,
    val fuelOrderId: String,
    val timeStamp: String,
    val fuelOrder: String
)



 @Entity
data class OfflineData (
    @PrimaryKey(autoGenerate = true) val uid: Int = 0,
    @ColumnInfo(name = "request_info_json") val requestInfoJson: String?,
    @ColumnInfo(name="request_code") val requestCode: String?,
    @ColumnInfo(name="request_info_jsonX") val requestInfoJsonX: OfflineDataRequestInfo
)

这不是我插入的

suspend fun insertOfflineData(requestInfoJson: String, requestCode: String, offlineDataRequestInfo: OfflineDataRequestInfo): Long {
        var result: Long = 0
        result = OfflineDatabaseManager.getInstance(app.applicationContext).insertOfflineData(
            OfflineData(
                0,
                requestInfoJson,
                requestCode,
                offlineDataRequestInfo
            ))
        return result
    }

offlineHelper.insertOfflineData(
                    inPositionApiData.toString(),
                    "notifyInPosition",
                OfflineDataRequestInfo(
                    "in-position",
                    action.userId,
                    id,
                    action.timestamp.toString(),
                    fuelOrderData.toString()
                ))

据我所知,您有两种解决方法:

  1. 为你的对象写一个TypeConverterOfflineDataRequestInfo
  2. 添加注释@Embedded
@Embedded
@ColumnInfo(name="request_info_jsonX") 
val requestInfoJsonX: OfflineDataRequestInfo