本文介绍了Android 房间持久库 - 如何插入具有 List 对象字段的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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); 我得到以下房间数据库编译错误:错误:(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 的表?如果是这样,如何告诉 room 在插入语句中连接它们?

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 注释:https://developer.android.com/training/data-storage/room/referencing-data.html#understand-no-object-references

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 对象以仅检索其权限数据;Languages 对象将转到另一个表,但您可以保留相同的 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:

这篇关于Android 房间持久库 - 如何插入具有 List 对象字段的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!