我正在尝试在单个操作中创建一个对象并填充该对象持有的外部集合的成员。我已经尝试了所有我能想到的操作顺序,但似乎无法让它发挥作用。
我的课程是(跳过不相关的字段和注释):
@DatabaseTable
public class ScheduledJob
{
@DatabaseField(id=true)
private String id = UUID.randomUUID ().toString ();
@ForeignCollectionField(eager=true)
private Collection<ScheduledJobTask> tasks;
/* other stuff here ... */
}
@DatabaseTable
public class ScheduledJobTask
{
@DatabaseField(id=true)
private String id = UUID.randomUUID ().toString ();
@DatabaseField(foreign=true)
private ScheduledJob job;
/* more other stuff ... */
public ScheduledJobTask(Task t) {
/* initialise scheduled task from template task by copying relevant fields...*/
}
}
我这样创建我的对象:
ScheduledJob job = new ScheduledJob ();
// fill in other fields of job here
job.tasks = new ArrayList<ScheduledJobTask> ();
for (Task t : template.getTasks())
job.tasks.add(new ScheduledJobTask(t));
sched.getSchedule().add (job); // 'sched' is an object loaded from another table, this
// adds 'job' to a foreign collection in that table
Storage.store.scheduledJobs.createIfNotExists (job);
for (ScheduledJobTask t : job.getTasks ())
Storage.store.scheduledJobTasks.createIfNotExists (t);
Storage.store.daySchedules.update (sched);
我已经尝试了最后三个语句的所有排序,但似乎没有任何效果:在每种情况下,最终在 schedulejobtask 数据库表中的条目的“job_id”字段都为空。
任何想法我做错了什么?