无法在1级android之后保存嵌套的ForeignCollectionField

时间:2017-07-04 19:00:39

标签: android ormlite

您好我正在尝试在数据库中保存嵌套的arrayList。

我使用 ForeignCollectionField 保存它,我已设法将其保存在数据库中。但是当我尝试从DB中检索时。它不会返回存储的数据。

您可以在突出显示的区域中的snapshot中看到。它在DB中保存。

但是当我尝试使用以下查询检索时

DatabaseManager.getInstance().
            getFirstMatching("surveyId", getIntent().getStringExtra("survey_id"), CampaignDetails.class);

它给出的结果如

this

以下是我用来保存数据库中的数据的代码

CampaignDetails.java

public class CampaignDetails extends UpdateableManagedObject {

    @DatabaseField
    private String campaignTitle;

    @DatabaseField
    private String surveyTitle;

    @DatabaseField
    private int surveyId;

    @ForeignCollectionField(eager = true, maxEagerLevel = 4)
    public ForeignCollection<QuestionDetail> questionDetail;

    public static void fromJson(JSONObject json) {
        if (json == null) return;
        int id = json.optInt("surveyId", 0);
        if (id == 0) return;

        CampaignDetails result = DatabaseManager.getInstance().getFirstMatching("surveyId", id, CampaignDetails.class);
        boolean created = result == null;
        if (created) {
            result = new CampaignDetails();
            result.surveyId = id;
        }

        result.questionDetail = result.getDao().getEmptyForeignCollection("questionDetail");
        result.campaignTitle = json.optString("campaignTitle");
        result.surveyTitle = json.optString("surveyTitle");
        result.hasCreated = true;
        try {
            fromJsonArray(json.getJSONArray("questionDetail"), result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (created) result.create();
        else result.update();
    }


    private static void fromJsonArray(JSONArray questionDetail, CampaignDetails campaignDetail) {

        for (int i = 0; i < questionDetail.length(); i++) {
            try {
                JSONObject jsonObject = questionDetail.getJSONObject(i);

                QuestionDetail result = DatabaseManager.getInstance().getFirstMatching("questionId", jsonObject.optString("questionId"), QuestionDetail.class);
                boolean isCreated = result == null;
                if (isCreated) {
                    result = new QuestionDetail();
                }
                result.question = jsonObject.optString("question");
                result.questionId = jsonObject.optString("questionId");
                result.questionType = jsonObject.optString("questionType");
                if (jsonObject.has("questionChoices")) {
                    result.questionChoices = result.getDao().getEmptyForeignCollection("questionChoices");
                    result.fromJson(jsonObject.getJSONArray("questionChoices"), result);
                }
                campaignDetail.questionDetail.add(result);
                result.campaignDetail = campaignDetail;
                if (isCreated) {
                    result.create();
                } else {
                    result.update();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


}

QuestionDetail.java

public class QuestionDetail extends ManagedObject {

    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    public CampaignDetails campaignDetail;

    @DatabaseField
    public String question;

    @DatabaseField
    public String questionId;

    @DatabaseField
    public String questionType;

    @ForeignCollectionField(eager = true,maxEagerLevel = 4)
    public Collection<QuestionChoice> questionChoices;

    public void fromJson(JSONArray choices, QuestionDetail questionDetail) {

        for (int i = 0; i < choices.length(); i++) {
            try {
                JSONObject jsonObject = choices.getJSONObject(i);

                QuestionChoice result = DatabaseManager.getInstance().getFirstMatching("choiceId", jsonObject.optString("choiceId"), QuestionChoice.class);
                boolean isCreated = result == null;
                if (isCreated) {
                    result = new QuestionChoice();
                }
                result.choiceId = jsonObject.optString("choiceId");
                result.choice = jsonObject.optString("choice");
                result.questionDetail = questionDetail;
                questionChoices.add(result);
                if (isCreated) {
                    result.create();
                } else {
                    result.update();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}

QuestionChoice.Java

public class QuestionChoice extends ManagedObject {

    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    public QuestionDetail questionDetail;

    @DatabaseField
    @SerializedName("choiceId")
    @Expose
    public String choiceId;

    @DatabaseField
    @SerializedName("choice")
    @Expose
    public String choice;

}

任何人都可以帮我解决问题吗?我真的被困在上面谢谢。

1 个答案:

答案 0 :(得分:0)

我已经通过更改下面两个文件中的代码顺序解决了这个问题

CampaignDetails.java

public class CampaignDetails extends UpdateableManagedObject {

    @DatabaseField
    private String campaignTitle;

    @DatabaseField
    private String surveyTitle;

    @DatabaseField
    private int surveyId;

    @ForeignCollectionField(eager = true, maxEagerLevel = 2)
    public ForeignCollection<QuestionDetail> questionDetail;

public ArrayList<QuestionDetail> getQuestionDetail() {
    if (assessmentCache == null) {
        refresh();
        assessmentCache = new ArrayList<>();

        if (questionDetail != null) {
            for (QuestionDetail assessment : questionDetail) {
                assessmentCache.add(assessment);
            }
        }
    }

    return assessmentCache;
}

public static void fromJson(JSONObject json) {
    if (json == null) return;
    int id = json.optInt("surveyId", 0);
    if (id == 0) return;

    CampaignDetails result = DatabaseManager.getInstance().getFirstMatching("surveyId", id, CampaignDetails.class);
    boolean created = result == null;
    if (created) {
        result = new CampaignDetails();
        result.surveyId = id;
        result.create();
    } else {
        result.update();
    }

    result.questionDetail = result.getDao().getEmptyForeignCollection("questionDetail");
    result.campaignTitle = json.optString("campaignTitle");
    result.surveyTitle = json.optString("surveyTitle");
    result.hasCreated = true;
    try {
        fromJsonArray(json.getJSONArray("questionDetail"), result);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    result.update();
}


private static void fromJsonArray(JSONArray questionDetail, CampaignDetails campaignDetail) {

    for (int i = 0; i < questionDetail.length(); i++) {
        try {
            JSONObject jsonObject = questionDetail.getJSONObject(i);

            QuestionDetail result = DatabaseManager.getInstance().getFirstMatching("questionId", jsonObject.optString("questionId"), QuestionDetail.class);
            boolean isCreated = result == null;
            if (isCreated) {
                result = new QuestionDetail();
            } else {
                continue;
            }
            result.questionChoices = result.getDao().getEmptyForeignCollection("questionChoices");
            result.campaignDetail = campaignDetail;
            result.question = jsonObject.optString("question");
            result.questionId = jsonObject.optString("questionId");
            result.questionType = jsonObject.optString("questionType");
            campaignDetail.questionDetail.add(result);
            if (jsonObject.has("questionChoices")) {
                result.fromJson(jsonObject.getJSONArray("questionChoices"), result);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

}

QuestionDetail.java

public class QuestionDetail extends ManagedObject {

    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    public CampaignDetails campaignDetail;

    @DatabaseField
    public String question;

    @DatabaseField
    public String questionId;

    @DatabaseField
    public String questionType;

    @ForeignCollectionField(eager = true, maxEagerLevel = 2)
    public Collection<QuestionChoice> questionChoices;

    public void fromJson(JSONArray choices, QuestionDetail questionDetail) {

        for (int i = 0; i < choices.length(); i++) {
            try {
                JSONObject jsonObject = choices.getJSONObject(i);

                QuestionChoice result = DatabaseManager.getInstance().getFirstMatching("choiceId", jsonObject.optString("choiceId"), QuestionChoice.class);
                boolean isCreated = result == null;
                if (isCreated) {
                    result = new QuestionChoice();
                } else {
                    continue;
                }
                result.questionDetail = questionDetail;
                result.choiceId = jsonObject.optString("choiceId");
                result.choice = jsonObject.optString("choice");
                questionDetail.questionChoices.add(result);
//                result.create();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}