如何通过查询自动获取外部DatabaseField?

时间:2016-05-24 19:25:11

标签: android ormlite android-database

我在我的Android应用中使用ORMLite(v4.48)。我有桌子"联系"它可以包含多个"电子邮件" (ForeignCollectionField)和一个" Personal" (DatabaseField)对象。当我从数据库中获取Contact对象时,我想自动获取(或延迟加载)具有相同联系ID的Personal对象。

它已经自动获取我可以访问的Email对象。但由于某种原因,Personal对象总是" null"即使个人表中有条目。

以下是我的课程:

@DatabaseTable(tableName = "Contact", daoClass = ContactDao.class)
public class Contact {

    @DatabaseField(generatedId = true, columnName = PersistentObject.ID) 
    int id;

    @DatabaseField(index = true)
    String contactName;

    @ForeignCollectionField(eager = false)
    ForeignCollection<Email> emails;

    @DatabaseField(foreign = true)
    public Personal personal;

    public ForeignCollection<Email> getEmails() {
        return emails;
    }

    public void setEmails(ForeignCollection<Email> emails) {
        this.emails = emails;
    }

    public Personal getPersonal() {
        return personal;
    }

    public void setPersonal(Personal personal) {
        this.personal = personal;
    }
    ...
}   

@DatabaseTable(tableName = "Email", daoClass = EmailDao.class)
public class Email {

    @DatabaseField(generatedId = true, columnName = PersistentObject.ID)
    int id;

    @DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = PersistentObject.CONTACT_ID_FIELD_NAME) // contact_id
    Contact contact;

    @DatabaseField
    String emailType;

    @DatabaseField(canBeNull = false)
    String email;

    public Email() {
    }

    public Email(int id, Contact Contact, String emailType, String email) {

        this.id = id;
        this.contact = contact;
        this.emailType = emailType;
        this.email = email;
    }

    public int getId() {

        return id;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public String getEmailType() {
        return emailType;
    }

    public void setEmailType(String emailType) {
        this.emailType = emailType;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    ...
}

@DatabaseTable(tableName = "Personal", daoClass = PersonalDao.class)
public class Personal {

    @DatabaseField(generatedId = true, columnName = PersistentObject.ID)
    int id;

    @DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = PersistentObject.CONTACT_ID_FIELD_NAME)
    Contact contact;

    @DatabaseField
    int age;

    @DatabaseField
    int weight; // in grams

    @DatabaseField
    int height; // in cm

    public Personal() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

}

我从数据库中获取数据如下:

QueryBuilder<Contact, Integer> queryBuilder = mContactDao.queryBuilder();
queryBuilder.orderBy("lastViewed", false);
queryBuilder.limit(limit);

PreparedQuery<Contact> preparedQuery = queryBuilder.prepare();
List<Contact> contactList = mContactDao.query(preparedQuery);
到目前为止一切运作良好。 然后在代码的下方,我可以像这样访问Email对象:

ForeignCollection<Email> emails = contact.getEmails();
Iterator<Email> iter = emails.iterator();
while (iter.hasNext()) {
    Email iAddress = iter.next();
    Log.d(TAG, "EMAIL: " + iAddress.getEmail());
    Log.d(TAG, "EMAIL TYPE: " + iAddress.getEmailType());
}

哪个也很完美。只有当我想访问Personal对象时,我总是得到NULL。

Personal personal = contact.getPersonal(); // is always NULL

我无法弄清楚为什么会这样。我是否需要手动在查询构建器中添加JOIN?我认为,一旦我使用getPersonal()访问它,就像使用getEmails()一样,它也会懒得加载数据?

1 个答案:

答案 0 :(得分:1)

您没有显示如何创建实体实例,但我假设在插入Contact之后创建了Personal。如果是这种情况,那么插入个人后你应该联系.setPersonal(个人),和contactDao.update(联系) - 这样个人id将存储在联系人行