3

我目前遇到以下情况;

基本上我有一个 Work 类,它有一个 ForeignCollection 的 WorkTasks。我想简单地接收链接到 Work 对象的所有 WorkTasks。

如果我查询所有 WorkTasks,我会得到一个结果列表,但带有“work = null”。因此它无法与正确的 Work 对象建立任何链接。查询 work_id 和 Work 本身的空列表时没有结果。

我已经无数次地看到过这样的例子和问题,但显然我错过了一些东西。

下面是我使用的相关代码;

数据库助手;

@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    try {

        applicantDao = DaoManager.createDao(connectionSource, Applicant.class);
        educationDao = DaoManager.createDao(connectionSource, Education.class);
        workDao = DaoManager.createDao(connectionSource, Work.class);
        workTaskDao = DaoManager.createDao(getConnectionSource(), WorkTask.class);
        onlinePersonDao = DaoManager.createDao(connectionSource, OnlinePerson.class);
        institutionDao = DaoManager.createDao(connectionSource, Institution.class);
        lessonDao = DaoManager.createDao(connectionSource, Lesson.class);

        TableUtils.createTable(connectionSource, Applicant.class);
        TableUtils.createTable(connectionSource, Education.class);
        TableUtils.createTable(connectionSource, Work.class);
        TableUtils.createTable(connectionSource, Institution.class);
        TableUtils.createTable(connectionSource, Lesson.class);
        TableUtils.createTable(connectionSource, OnlinePerson.class);
        TableUtils.createTable(connectionSource, Reference.class);
        TableUtils.createTable(connectionSource, WorkTask.class);

[....]

 public Dao<WorkTask, Integer> getWorkTaskDao() {
    if (null == workTaskDao) {
        try {
            workTaskDao = getDao(WorkTask.class);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }
    return workTaskDao;
}

数据库管理员:

public List<Experience> getAllWork() {
        List<Experience> exp = null;
        try {
            exp = getHelper().getWorkDao().queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return exp;
    }

    public List<WorkTask> getAllWorkTask() {
        List<WorkTask> workTask = null;
        try {
            workTask = getHelper().getWorkTaskDao().queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return workTask;
    }

public List<WorkTask> getWorkTaskByWorkId(int workId) {
        List<WorkTask> workTasks = null;
        try {
            workTasks = getHelper().getWorkTaskDao().queryForEq("work_id", workId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return workTasks;
    }

public void addWork(Collection<Work> jobs) {
        try {
            for (Experience work : jobs) {
                Work w = (Work) work;
                // Add nested child first
                this.addInstitution(w.institution);
                this.addWorkTask(w.tasks);

                getHelper().getWorkDao().createOrUpdate(w);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addWorkTask(Collection<WorkTask> worktasks) {
        try {
            for (WorkTask wt : worktasks) {
                getHelper().getWorkTaskDao().createOrUpdate(wt);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

工作模型的列表(从抽象父级获取预填充的 id):

@ForeignCollectionField(eager = true)
    @SerializedName("tasks")
    public Collection<WorkTask> tasks;

    public ArrayList<WorkTask> getTasks(){
        ArrayList<WorkTask> taskList = new ArrayList<WorkTask>();
        Iterator iterator = tasks.iterator();
        while(iterator.hasNext()){
            WorkTask task = (WorkTask) iterator.next();
                    taskList.add(task);

        }
        return taskList;
    }

工作任务:

public class WorkTask {

    /**
     * Auto-incremented id for the ORMLite-SQLite database
     */
    @DatabaseField(generatedId = true)
    public int id;

    /**
     * Foreign field id for the ORMLite-SQLite database
     */
    @DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true, columnName = "work_id")
    public Work work;

最后是所有让我失望的事情:

        ArrayList<WorkTask> tasks_iterated = work.getTasks();

        ArrayList<WorkTask> tasks_id = (ArrayList<WorkTask>) DatabaseManager.getInstance()
                .getWorkTaskByWorkId(work.id);

        ArrayList<WorkTask> tasks = (ArrayList<WorkTask>) DatabaseManager.getInstance().getAllWorkTask();

这最终给我留下了:

tasks_iterated = empty
tasks_id = empty
tasks = 我的任务的完整列表,但所有任务都具有属性“work = null”,因此我无法将它们放置到正确的 Work 对象中。

4

1 回答 1

2

通过将我的添加方法更改为:

public void addWorkTask(Collection<WorkTask> worktasks, Work work) {
        try {
            for (WorkTask wt : worktasks) {
                wt.work = work;
                getHelper().getWorkTaskDao().createOrUpdate(wt);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

不确定这是否是唯一的方法。似乎有点奇怪,我必须手动执行此操作。

于 2012-12-10T18:09:46.823 回答