When calling toModel() all members are empty
I have the following code. The only problem I'm having right now is that when I call getQuestionnaire() it returns a non null Questionnaire object, with the correct id, but empty members. Am I doing something wrong? Is there something I didn't get?
@ModelContainer
@Table(databaseName = Database.NAME)
public class QuestionnaireOutput extends BaseModel {
@Column
@PrimaryKey(autoincrement = true)
long id;
@Column
boolean sent;
@Column
boolean finished;
@Column
double latitude;
@Column
double longitude;
@Column
@ForeignKey(
onDelete = ForeignKeyAction.CASCADE,
onUpdate = ForeignKeyAction.CASCADE,
references = {@ForeignKeyReference(columnName = "questionnaire_id",
columnType = Long.class,
foreignColumnName = "id")},
saveForeignKeyModel = false)
ForeignKeyContainer<Questionnaire> questionnaire;
public QuestionnaireOutput() {
}
public QuestionnaireOutput(Questionnaire questionnaire) {
this(false, questionnaire);
}
public QuestionnaireOutput(boolean sent, Questionnaire questionnaire) {
this.sent = sent;
this.finished = false;
setQuestionnaire(questionnaire);
}
public void setQuestionnaire(Questionnaire model) {
questionnaire = new ForeignKeyContainer<>(Questionnaire.class);
questionnaire.put(Questionnaire$Table.ID, model.id);
}
public Questionnaire getQuestionnaire(){
return questionnaire.toModel();
}
}
@strigare when you call toModel() what is the value in the ForeignKeyContainer? also the result of the query?
I'm having the same issue (this should be labeled as such). The only filled member is the foreign key (the id of the referenced model). The others are empty.
@Table(databaseName = WADatabase.NAME)
public class PrivateRoute extends Route{
@Column
@PrimaryKey
@SerializedName("id")
public long _id;
@Column
private int numDays;
@Column
@ForeignKey(references = {@ForeignKeyReference(columnName = "id",
columnType = Long.class,
foreignColumnName = "_id")},
saveForeignKeyModel = false)
public ForeignKeyContainer<Destination> destination;
public PrivateRoute() {
}
public int getNumDays() {
return numDays;
}
public void setNumDays(int days) {
this.numDays = days;
}
public long getId() {
return _id;
}
public void setId(int id) {
this._id = id;
}
public void associateDestination(Destination destination){
this.destination = new ForeignKeyContainer<>(Destination.class);
this.destination.setModel(destination);
this.destination.put(Destination$Table._ID, destination.getId());
}
@Override
public String toString() {
String destinationString = (destination != null) ?
"(" + destination.toModel().getId() + ") " + destination.toModel().getName() : null;
return "PrivateRoute{" +
"_id=" + _id +
", numDays=" + numDays +
", destination=" + destinationString +
'}';
}
}
toString() output is
PrivateRoute{_id=141, numDays=11, destination=(3294) null}
Note: Route extends BaseModel, and Destination has the @ModelContainer annotation.
Try in 3.0/develop to see if it's fixed, otherwise I'll close this.
I'm experiencing this issue even into a plain sample made according to the docs (queen, ants and so..) under 2.2.1.
I've tried to check 3.0.0 but it is not available via jcenter and a local checkout+install+mavenLocal takes me to other issues.
Any update/workaround on this?
you should call load() on a ForeignKeyContainer to get the full object that's stored in another table. The docs in develop for ForeignKeyContainer have been updated to reflect this.