Stefan de Bruijn
Stefan de Bruijn

Reputation: 6319

Android ORMLite - ForeignCollection child has null foreign field

I'm currently stuck with the following situation;

Basically I've got a Work class, which has a ForeignCollection of WorkTasks. I'd like to simply receive all WorkTasks, linked to Work object.

If I query for all WorkTasks, I do get a list of results but with 'work = null'. So it can't make any link to the correct Work object. Resulting in no results with querying for the work_id and an empty list in Work itself.

I've seen examples and questions about this countless of times but apparently im missing out on something.

Below is the code that im using which is relevant;

The DatabaseHelper;

@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;
}

The database manager:

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();
        }
    }

The list from the work model (gets a pre-filled id from an abstract parent):

@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;
    }

The WorkTask :

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;

And finally all the things that are failing me:

        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();

This eventually leaves me with:

tasks_iterated = empty
tasks_id = empty
tasks = a full list of my tasks but all with the attribute 'work = null' so I can't place them to the correct Work object.

Upvotes: 3

Views: 2224

Answers (1)

Stefan de Bruijn
Stefan de Bruijn

Reputation: 6319

Fixed it by changing my adding method to:

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();
        }
    }

Not sure if it's the only way to do this though. Seems a bit weird i'd have to do this manually.

Upvotes: 2

Related Questions