Ormlite android - Query object has foreign collection with conditions

1,132 views
Skip to first unread message

Pham Tien Thanh

unread,
Apr 12, 2013, 9:09:52 AM4/12/13
to ormlit...@googlegroups.com
I'm learning to use your ORMLite framework for Android.
I'm very exciting with it.
But I faced a problem that I tried many ways to resolve but failed.
For example.
I have 2 database classes:

@DatabaseTable(tableName = "Subject")
public class Subject {
        @DatabaseField(columnName = "id", id = true)
        protected String id;

        @DatabaseField(columnName = "deleteFlag", canBeNull = false, defaultValue = "0")
        protected boolean deleteFlag = false;

        @DatabaseField(columnName = "name", canBeNull = false)
        private String name;

        @ForeignCollectionField(eager = true, maxEagerLevel = 3)
        private Collection<Student> students;
        ...
}

@DatabaseTable(tableName = "Student")
public class Student {
        @DatabaseField(columnName = "id", id = true)
        protected String id;

        @DatabaseField(columnName = "deleteFlag", canBeNull = false, defaultValue = "0")
        protected boolean deleteFlag = false;

        @DatabaseField(columnName = "firstName", canBeNull = false)
        private String firstName;

        @DatabaseField(columnName = "lastName", canBeNull = true)
        private String lastName;

        @DatabaseField(columnName = "subjectId", canBeNull = false, foreign = true, foreignColumnName = "id")
        private Subject subject;

        ...
}

It requires that the record is not deleted in database, it just be marked by deleteFlag column.

Now, I try to query the all subjects which have "deleteFlag = false". Each subject has list of students which have "deleteFlag = false" too.
Is there a way to query using SubjectDao???
Or I have to do 2 steps:
1. Using SubjectDao to query all the subjects which have "deleteFlag = false";
2. Loop in list of subjects, using StudentDao to query all students of that subject which have "deleteFlag = false". Then set list of students to the subject object.

Thanks for your time.

Gray Watson

unread,
May 24, 2013, 8:23:28 PM5/24/13
to ormlit...@googlegroups.com
Sorry for the delay on this.

On Apr 12, 2013, at 9:09 AM, Pham Tien Thanh <dr.p...@gmail.com> wrote:

> It requires that the record is not deleted in database, it just be marked by deleteFlag column.
>
> Now, I try to query the all subjects which have "deleteFlag = false". Each subject has list of students which have "deleteFlag = false" too.
> Is there a way to query using SubjectDao???

Yes using the join mechanism. See the join docs:

http://ormlite.com/docs/join-queries

gray



Reply all
Reply to author
Forward
0 new messages