Reputation: 105
I'm new to android and ormlite and i have creating an database CRUD operation with One to Many Relationship. The problem is i can't save the Collection data from Parent Class to child collections data into database. Here is my code :
(Parent class)
@DatabaseTable
public class Schedule implements Serializable {
private static final long serialVersionUID = 1L;
@DatabaseField(generatedId = true)
public int id;
@DatabaseField
public Date date;
@DatabaseField
public String patient;
@DatabaseField
public String room;
@ForeignCollectionField(eager = false)
public Collection<Task> tasks = new ArrayList<Task>();
public Schedule() {
}
}
(Child Class)
@DatabaseTable
public class Task implements Serializable {
private static final long serialVersionUID = 1L;
@DatabaseField(generatedId = true)
public int id;
@DatabaseField
public Date time;
@DatabaseField
public boolean done;
@DatabaseField
public String note;
@DatabaseField(foreign=true,foreignAutoRefresh=true)
public Schedule schedule = new Schedule();
public Task() {
}
}
When i save the data (Schedule class) with collection (tasks), it don't save the collection. Am i missing something ? Thanks.
Upvotes: 1
Views: 3419
Reputation: 116888
When i save the data (Schedule class) with collection (tasks), it don't save the collection. Am i missing something?
So there is no easy way for ORMLite to know that your collection of tasks has not already been inserted into the database. If you need to create them they you should either do that yourself or initialize the tasks
field with:
parentDao.assignEmptyForeignCollection(parent, "tasks");
This will turn your tasks
field into a foreign collection so any add(...)
calls to it will be added also to the database.
Upvotes: 1