使用ORMLite在Android问题节能系列 [英] Problems saving Collection using ORMLite on Android
问题描述
我有两个类:
public class Questionnaire {
@DatabaseField(generatedId=true, useGetSet=true)
private Long id;
@DatabaseField
private int type;
@DatabaseField
private String title;
@DatabaseField
private String description;
@ForeignCollectionField(eager = true)
private Collection<Question> questions;
// Get and Set omitted
和
public class Question {
@DatabaseField(generatedId=true, useGetSet=true)
private Long id;
@DatabaseField
private int type;
@DatabaseField
private String description;
@DatabaseField(foreign = true, foreignAutoRefresh= true)
private Questionnaire questionario;
//get and set ommited
当我保存问题列表的问卷调查。这些对象持久化,但我输的关系。
When I save a Questionnaire with a List of Questions. The objects are persisted, but I lose the relationship.
我保存了这种方式:
ForeignCollection<Question> questions =
getDao(Questionnaire.class).getEmptyForeignCollection("questions");
for(Question question : DataUtil.getAllQuestions()) {
questions.add(question);
}
Questionnaire questionnarie = new Questionnaire();
questionnarie.setQuestions(questions);
questionnarie.setTitle("Normal");
questionnarie.setDescription("Questionário normal");
getDao(Questionnaire.class).createOrUpdate(questionarie);
当我从检索数据库这个寄存器,一个问题
数据不具有问卷
和参考我的问卷
没有问题列表填充。
When I retrieved this register from database, a Question
data doesn't have a reference for Questionnaire
, and my Questionnaire
doesn't have question list filled.
任何帮助将AP preciated。
Any help will be appreciated.
推荐答案
现在的问题是,你不设置你的问题上的
的对象。的关系是从 questionario
字段问题
的关联问卷
。没有任何的问卷
表指向的其他方式。请参阅文档外国对象。
The problem is that you are not setting the questionario
field on your Question
objects. The relationship is from the Question
to the associated Questionnaire
. There is nothing in the Questionnaire
table that points the other way. See the documentation on foreign objects.
我会建议做一些这样的:
I would recommend doing something like the following:
Dao<Questionnaire, Long> dao = getDao(Questionnaire.class);
ForeignCollection<Question> questions =
dao.getEmptyForeignCollection("questions");
Questionnaire questionnarie = new Questionnaire();
questionnarie.setQuestions(questions);
questionnarie.setTitle("Normal");
questionnarie.setDescription("Questionário normal");
dao.createOrUpdate(questionarie);
for(Question question : DataUtil.getAllQuestions()) {
// you must set the questionnarie field on the Question
// if it is a generated-id, it must be set _after_ it has been created
question.setQuestionnaire(questionnarie);
questions.add(question);
}
这篇关于使用ORMLite在Android问题节能系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!