Android ORMLite 在保存或不刷新时没有设置外语

Android ORMLite not setting foreign when saving or not refreshing

简介

我有两个实体:

特质

@DatabaseTable(tableName = "traitement", daoClass = TraitementNettoyageDAO.class)
public class TraitementEntity implements Parcelable {


    @DatabaseField(generatedId = true)
    private int id;
    @ForeignCollectionField(eager = true)
    private Collection<AnomalieNettoyageEntity> mListAnomalieNettoyage;

    public TraitementEntity() {
    }

    // omitting getter/setter and parcelable job
}

异常

@DatabaseTable(tableName = "anomalies", daoClass = AnomalieDAO.class)
public class AnomalieEntity implements Parcelable {


    @DatabaseField(generatedId = true)
    private int id;

    @DatabaseField
    private String mIdAnomalie;


    @DatabaseField(foreign = true, columnName = "traitementForeignId", foreignAutoRefresh = true, canBeNull = false)
    private TraitementEntity traitementForeign;

    // omitting getter/setter and parcelable job
}

我实际上是在使用 :

将我的特征保存在数据库中
function void save(TraitementEntity obj){
 for (AnomalieEntity n : obj.getmListAnomalie()) {
                n.setTraitementForeign(obj);
            }

    create(obj);
}

问题

问题是当我需要在本地数据库中保存我的特征时,我似乎什么都没有:

Log.i("OBJECT ANOMALIE SIZE", obj.getmListAnomalie().size() + ""); // gives 3
save(obj);

List<TraitementEntity> liste = mServiceLocal.getAllLocalTraitements();
for (TraitementEntity n : liste)
    Log.i("OBJECT ANOMALIE SIZE 2", n.getmListAnomalie().size() + ""); // gives always 0

/* This getmListeAnomalie() corresponds to the standard try/catch for queryForAll(), nothing else */

特征保存完好,外来异常没保存。 所以它部分工作,我不知道为什么我有这种行为。

你能帮帮我吗?

您可以尝试将 foreignAutoCreate = true 设置为 DatabaseField 注释,如下所示:

@DatabaseField(foreign = true, columnName = "traitementForeignId", foreignAutoRefresh = true, foreignAutoCreate = true, canBeNull = false)

这是他们docs的解释:

Set this to be true (default false) to have the foreign field will be automagically created using its internal DAO if the ID field is not set (null or 0).

确认问题。

在他的TraitementEntity中class不包含getmListAnomalie()方法 它与此不同:

for ( n AnomalieEntity : obj.getmListAnomalie ()) 

不会

for ( AnomalieNettoyageEntity n : obj.mListAnomalieNettoyage ()) {
    n.setTraitementForeign ( obj) ;
}

其他选项。 在托管示例(SqlLite 浏览器)中加载数据库 SqlLite 并检查表已填充!

请确认,测试!