Android Room持久性库-如何插入具有List对象字段的类 [英] Android room persistent library - how to insert class that has a List object field
问题描述
在 Android会议室持久性库中,如何将整个Model对象插入本身具有另一个列表的表.
In Android room persistent library how to insert entire Model object into table which has in itself another list.
让我告诉你我的意思:
@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;
}
}
我的DAO看起来像这样:
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);
}
当我调用database.CountriesDao().addCountries(countryModel);
时,出现以下Room db编译错误:
错误:(58,31)错误:无法弄清楚如何将该字段保存到数据库中.您可以考虑为其添加类型转换器.
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.
是否应该有另一个名为CountryLang的表?如果可以的话,如何告诉房间在插入语句上将它们连接起来?
should there be another table called CountryLang ? and if so how to tell room to connect them on insert statement ?
CountryLang对象本身如下所示:
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;
}
}
响应如下:
"country_lang": [
{
"id_country": 2,
"id_lang": 1,
"name": "Austria"
}
]
对于每个国家/地区来说,这里的内容不会多于一项.我不愿意在country_lang列表中仅对其中一项进行设计.因此,我可以为country_lang创建一个表,然后将其链接到CountryModel.但是如何?我可以使用外键吗?我希望我不必使用平面文件.所以你说我必须将其存储为json吗?是否建议不要暂时使用房间?改用什么?
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 ?
推荐答案
正如Omkar所说,您不能.在这里,我根据文档描述了为什么应该始终使用@Ignore
批注:
As Omkar said, you cannot. Here, I describe why you should always use @Ignore
annotation according to the documentation: https://developer.android.com/training/data-storage/room/referencing-data.html#understand-no-object-references
您将在一个表中处理Country对象,以仅检索其能力的数据;语言对象将转到另一个表,但是您可以保留相同的Dao:
You will treat the Country object in a table to retrieve the data of its competence only; The Languages objects will go to another table but you can keep the same Dao:
- 国家和语言对象是独立的,只需在语言"实体(countryId,languageId)中定义带有更多字段的primaryKey即可.当活动线程是Worker线程时,可以将它们串联保存在Repository类中:两个对Dao的插入请求.
- 要加载Countrys对象,您需要有countryId.
- 要加载相关的Languages对象,您已经具有countryId,但是您需要先加载该国家/地区,然后再加载语言,以便可以在父对象中设置它们并仅返回父对象.
- 加载国家/地区时,您可以在Repository类中依次进行此操作,因此,您将同步加载国家/地区和语言,就像在服务器端一样! (没有ORM库).
这篇关于Android Room持久性库-如何插入具有List对象字段的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!