How to create #Entity class by using room with Map<> - android

How to create #Entity class by using room with Map<>
I have such data class:
data class ListResponse(
#SerializedName("answer")
val answer: Map<Int, ItemList>
)
data class ItemList(
#SerializedName("yes")
val yes: YesItem,
#SerializedName("no")
val no: NoItem
)
data class YesItem(
#SerializedName("time")
val timeYes: String,
#SerializedName("date")
val dateYes: String
)
data class NoItem(
#SerializedName("time")
val timeNo: String,
#SerializedName("date")
val dateNo: String
)
and I need data base with such strucure:
Primary key must be taken as Int key from Map
Map Int key|timeYes|dateYes|timeNo|dateNo
something like that:
#Entity(tableName = TABLE_NAME)
data class PromotionListEntity(
#PrimaryKey
#SerializedName("id") // I don't know SerializedName for Int value
val id: //answer Map Int key
#SerializedName("timeYes")
val timeYes: String,
#SerializedName("dateYes")
val dateYes: String,
#SerializedName("timeNo")
val timeNo: String,
#SerializedName("dateNo")
val dateNo: String,
){
companion object{
const val TABLE_NAME = "answer_table"
}
}

Related

RoomDB Kotlin enum needs a constructor any way around this?

I am getting this error
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). public enum Measurement {
the enum will simply be a way to store answers from a dropdown.
#Entity(tableName = "bottles")
data class Bottle(
#PrimaryKey (autoGenerate = true)
val consumableID: String,
#Embedded var prescription : Prescription?,
#Embedded var measurement: Measurement?,
#Embedded var reminder: Reminder?,
var quantityInBottle: Int?,
val expirationDate : Date?,
var startDate : Date?,
val cabinetID: String
)
enum class Measurement {
MILLIGRAMS,
SCOOP,
GRAMS,
OZ,
TSP,
TBS,
}
I have TypeConverters for measurement defined as
class MeasurementTypeConverters {
#TypeConverter
fun measurementToString(measurement: Measurement?) : String{
return measurement.toString()
}
#TypeConverter
fun stringToMeasurement(string: String) : Measurement{
return Measurement.valueOf(string)
}
}

Android Room: Entities and POJOs must have a usable public constructor

I'm trying to save the data class i got from json to room database and suddenly get errors like this while building the app.
Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
NewsResponse model object
data class NewsResponse(
val copyright: String,
val response: Response,
val status: String
)
Response object
data class Response(
val docs: List<Docs>
)
Docs object(this is the data class i want to save)
#Entity(
tableName = "docs"
)
data class Docs(
#PrimaryKey(autoGenerate = true)
var id: Int? = null,
val _id: String,
val `abstract`: String,
#Embedded
val byline: Byline,
val document_type: String,
#Embedded
val multimedia: Multimedia,
#Embedded
val headline: Headline,
val lead_paragraph: String,
val news_desk: String,
val pub_date: String,
val section_name: String,
val snippet: String,
val source: String,
val subsection_name: String,
val type_of_material: String,
val uri: String,
val web_url: String,
val word_count: Int
)
Byline object
#Entity
data class Byline(
val original: String
)
Multimedia object
#Entity
data class Multimedia(
val url: String
)
Headline object
#Entity
data class Headline(
val main: String
)
Any ideas how to solve this?

Adding a POJO into a Room database table

I have a POJO class OfflineDataRequestInfo which I want to insert into a database table as part of OfflineData which is my entity. but I get an error
How can I fix this please
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;
this is my code
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
)
This is not I insert
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()
))
As i see you have 2 way to solve it :
Write a TypeConverter for your object OfflineDataRequestInfo
Add annotation #Embedded
#Embedded
#ColumnInfo(name="request_info_jsonX")
val requestInfoJsonX: OfflineDataRequestInfo

ObjectBox Always return null for inner data classes in Kotlin

I am using Retrofit for API Calls.
I was able to cache data class which contains only strings and int.
But now I want to cache another model class which has nested model classes like
below.
#Entity data class ConsumptionDashboardResponse(
#Id(assignable = true)
var id: Long? = null,
#Transient
#SerializedName("data")
#NameInDb("data_consumption")
var `data`: DataConsumption,
#SerializedName("responseCode")
val responseCode: Int,
#SerializedName("responseDesc")
val responseDesc: String,
#SerializedName("sessionLang")
val sessionLang: String )
DataConsumption Class:
#Entity
data class DataConsumption(
#Id(assignable = true)
var id: Long? = null,
#Backlink
#Transient
#SerializedName("listDivisions")
var listDivisions: List<Divisions>
)
And Divisions Class
#Entity
data class Divisions(
#Id(assignable = true)
var id: Long? = null,
#SerializedName("aggregateBalance")
val aggregateBalance: String,
#SerializedName("aggregateConsumption")
val aggregateConsumption: String,
#SerializedName("division")
val division: String,
#Backlink
#Transient
#SerializedName("listConsumption")
var listConsumption: List<Consumption>,
#SerializedName("unit")
val unit: String
)
Divison class contains list of Consumption
#Entity
data class Consumption(
#Id(assignable = true)
var id: Long? = null,
#SerializedName("aggregateConsumption")
val aggregateConsumption: String,
#SerializedName("billingPeriod")
val billingPeriod: String
)
I am unable to find if I need any type of custom converters or what else I need to do.
If I try to read saved data from ObjectBox this is what I get:
ConsumptionDashboardResponse(id=4, data=null, responseCode=200, responseDesc=SUCCESS, sessionLang=AR)
A Transient field is not persisted in the database. If you have to store properties of non-primitive type either use relationships or create type converters.

Cannot figure out how to save this field into database, save a list of Integer

I have this entity :
#Entity(tableName = "recipes")
data class Recipe(
#PrimaryKey val id: Int,
val name: String,
#TypeConverters(Converters::class)
val categories: List<Int>,
val picture: String,
#Embedded(prefix = "time")
val time: Time,
val people: Int,
val difficulty: Int,
val price: Int,
#Embedded(prefix = "ingredients")
#TypeConverters(Converters::class)
val ingredients: List<Ingredient>,
#Embedded(prefix = "utensils")
#TypeConverters(Converters::class)
val utensils: List<Utensil>,
#Embedded(prefix = "steps")
#TypeConverters(Converters::class)
val steps: List<Step>,
val createdAt: String,
val updatedAt: String
) : Serializable
And when I compile it, I got
Cannot figure out how to save this field into database. You can consider adding a type converter for it.
private final java.util.List<java.lang.Integer> categories = null;
Problem is, I added the TypeConverter, it's in my class Converters :
#TypeConverter
fun fromIntList(value: List<Int>): String {
val gson = Gson()
val type = object : TypeToken<List<Int>>() {}.type
return gson.toJson(value, type)
}
#TypeConverter
fun toIntList(value: String): List<Int> {
val gson = Gson()
val type = object : TypeToken<List<Int>>() {}.type
return gson.fromJson(value, type)
}
It should work since it knows that it should transforma list of Int to strings.
Futhermore, I got this error which I dont know where it can come from, and I have it like 5 times.
Entities and Pojos must have a usable public constructor. You can have an empty
constructor or a constructor whose parameters match the fields (by name and type).
I added the nested class like Ingredient, Step, Ustensil but I dont post the code since it should not be related.

Categories

Resources