I am parsing json data using Jackson from api call and trying to insert the data in sqlite using ormlite. For this i am using same model class. Here is my model classes
public class Site {
@DatabaseField(generatedId=true,columnName="ID")
private int id;
@DatabaseField(columnName="NAME")
private String name;
@ForeignCollectionField(eager=true)
Collection<Visit> visits;
Site() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
public Collection<Visit> getVisits() {
return visits;
}
@JsonProperty("visits")
public void setVisits(Collection<Visit> visits) {
this.visits = visits;
}
}
Here is visit model class
@DatabaseTable(tableName = "VISIT")
public class Visit {
@DatabaseField(generatedId=true,columnName="ID")
private int id;
@DatabaseField(columnName="VISIT_DATE")
private Date visitDate;
@ForeignCollectionField(eager=false)
Collection<CheckListItem> checklistItems;
@DatabaseField(foreign=true,foreignAutoRefresh=true,columnName="FOREIGN_ID")
private Site site;
Visit(){}
public Site getSite() {
return site;
}
public void setSite(Site site) {
this.site = site;
}
public Date getVisitDate() {
return visitDate;
}
@JsonProperty("date")
public void setVisitDate(Date visitDate) {
this.visitDate = visitDate;
}
public Collection<CheckListItem> getChecklistItems() {
return checklistItems;
}
@JsonProperty("checklist_items")
public void setChecklistItems(Collection<CheckListItem> checklistItems) {
this.checklistItems = checklistItems;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
I can parse the json data to list of site objects by following:
List siteList = objectMapper.readValue(jp, new TypeReference>(){});
And this worked perfectly. Now i want to store all this data(list of Site objects) in the db by using ormlite(with eager foreignCollection). For this i am trying like this:
for each site in siteList
Collection<Visit> vTemp=site.getVisits();
Dao<Site, Integer> siteListDao=getHelper().getSiteListDao();
s.visits=siteListDao.getEmptyForeignCollection("visits");
siteListDao.create(site);
for(Visit v:vTemp)
s.contacts.add(v);
Its inserting the data to the SITE and VISIT table. BUt when i try to fetch data by queryForAll() function on SITE table it returns a foreignCollection object(site.getVisits()) but its size is 0 although i have set the eager=true.
I am asuming i have done something wrong with inserting. Please tell me what is the proper way of doing this insertion using same model class object.