Reputation: 3730
I'm working on dictionnary application in which the api request have many nested lists , i have tried to insert all the nested lists but i'm getting different error each time , i would like to know what is the best way to save multiple nested lists , should i use room relations or something else , thank you in advance for help , i m really stuck with this for few days now
@Entity(tableName = "DICTIONNARYTABLE")
@TypeConverters(DictionnaryModelConverter::class)
class DictionnaryModel : ArrayList<DictionnaryModelItem>() {
@PrimaryKey(autoGenerate = true)
@NotNull
val wordId: Long = 0
}
@Entity
data class DictionnaryModelItem(
@PrimaryKey val dictionnaryModelId: Long = 0,
@TypeConverters(DictionnaryMeaningConverter::class)
val meanings: MutableList<Meaning>,
@TypeConverters(DictionnaryPhoneticsConverter::class)
val phonetics: MutableList<Phonetic>,
val word: String
)
//---------------------------
@Entity
data class Meaning(
@PrimaryKey val meaningId: Long = 0,
@TypeConverters(DictionnaryDefinitionConverter::class)
val definitions: List<Definition>,
val partOfSpeech: String
)
///-------------------------------
@Entity
data class Phonetic(
@PrimaryKey val phoneticId: Long = 0,
val audio: String,
val text: String
)
@Entity
data class Definition(
@PrimaryKey val definitionId: Long = 0,
val definition: String,
val example: String,
@TypeConverters(DictionnarySynonymsConverter::class)
val synonyms: List<String>
)
Upvotes: 3
Views: 1729
Reputation: 1574
You need to create one-to-many relationship data model here. For instance each dictionary word has many meanings and many phonetics. Here Dictionary is a parent entity and Meaning and Phonetic are the child entities. Each child entity will have it's parent entity primary key stored in its table. You will need another data class to define this relationship.
data class DictionaryWithMeanings(
@Embedded val dictionary: Dictionary,
@Relation(
parentColumn = "dictionaryModelId",
entityColumn = "dictionaryId"
)
val meanings: List<Meaning>
)
Meaning table has to store dictionaryId as foreign key its table. Same has to be defined for phonetics. And Meaning table again has similar relationship with Definition and so on.
Upvotes: 3