In Android room persistent library how to insert entire Model object into table which has in itself another list.
Let me show you what i mean :
@Entity(tableName = TABLE_NAME) public class CountryModel { public static final String TABLE_NAME = "Countries"; @PrimaryKey private int idCountry; private List<CountryLang> countryLang = null; public int getIdCountry() { return idCountry; } public void setIdCountry(int idCountry) { this.idCountry = idCountry; } public String getIsoCode() { return isoCode; } public void setIsoCode(String isoCode) { this.isoCode = isoCode; } /** here i am providing a list of coutry information how to insert this into db along with CountryModel at same time **/ public List<CountryLang> getCountryLang() { return countryLang; } public void setCountryLang(List<CountryLang> countryLang) { this.countryLang = countryLang; } }
my DAO looks like this:
@Dao public interface CountriesDao{ @Query("SELECT * FROM " + CountryModel.TABLE_NAME +" WHERE isoCode =:iso_code LIMIT 1") LiveData<List<CountryModel>> getCountry(String iso_code); @Query("SELECT * FROM " + CountryModel.TABLE_NAME ) LiveData<List<CountryModel>> getAllCountriesInfo(); @Insert(onConflict = REPLACE) Long[] addCountries(List<CountryModel> countryModel); @Delete void deleteCountry(CountryModel... countryModel); @Update(onConflict = REPLACE) void updateEvent(CountryModel... countryModel); }
When i call database.CountriesDao().addCountries(countryModel);
i get the following room db compile error: Error:(58, 31) error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
should there be another table called CountryLang ? and if so how to tell room to connect them on insert statement ?
The CountryLang object itself looks like this:
public class CountryLang { private int idCountry; private int idLang; private String name; public int getIdCountry() { return idCountry; } public void setIdCountry(int idCountry) { this.idCountry = idCountry; } public int getIdLang() { return idLang; } public void setIdLang(int idLang) { this.idLang = idLang; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
the response looks like this:
"country_lang": [ { "id_country": 2, "id_lang": 1, "name": "Austria" } ]
For every country so its not going to be more then one item here. Im comfortable desgning it for just one item in the country_lang list. So i can just make a table for country_lang and then some how link it to CountryModel. but how ? can i use foreign key ? i was hoping i did not have to use a flat file. so your saying i have to store it as json ? Is it recommended not to use room for temporary ? what to use instead ?
You can easly insert the class with list object field using TypeConverter and GSON,
public class DataConverter { @TypeConverter public String fromCountryLangList(List<CountryLang> countryLang) { if (countryLang == null) { return (null); } Gson gson = new Gson(); Type type = new TypeToken<List<CountryLang>>() {}.getType(); String json = gson.toJson(countryLang, type); return json; } @TypeConverter public List<CountryLang> toCountryLangList(String countryLangString) { if (countryLangString == null) { return (null); } Gson gson = new Gson(); Type type = new TypeToken<List<CountryLang>>() {}.getType(); List<CountryLang> countryLangList = gson.fromJson(countryLangString, type); return countryLangList; } }
Next, Add the @TypeConverters annotation to the AppDatabase class
@Database(entities = {CountryModel.class}, version = 1) @TypeConverters({DataConverter.class}) public abstract class AppDatabase extends RoomDatabase { public abstract CountriesDao countriesDao(); }
For more information about TypeConverters in Room check our blog here and the official docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With