Realm.io - 是否有可能通过其子对象找到对象?

时间:2015-05-08 12:25:09

标签: android realm

医生包括对象组织子对象:

@PrimaryKey
private int doctorId;
private FullName fullName;
private Age age;
private Organization organization;
private Position position;
private String category;
private String loyalty;
private List<Specialization> specializations;
private Contacts contacts;

组织模型具有以下参数:

    @PrimaryKey
    private OrganizationId organizationId;
    private String organizationName;
    private String key;
//    private Address address;
    private String address;
    private String phoneNumber;

像这样填充值:

Organization organization = realm.createObject(Organization.class); // Create a new object
OrganizationId organizationId = realm.createObject(OrganizationId.class);
organizationId.setAggregateId("1");
organization.setOrganizationId(organizationId);
organization.setOrganizationName("1-я Клиника Ташкентской Медицинской Академии");
organization.setAddress("Адрес: г. Ташкент, ул. Фароби, 2");
organization.setPhoneNumber("Тел.: (+99871) 214-51-01, 214-50-86, 214-50-43");
organization.setKey(organization.getOrganizationName().toLowerCase());

Doctor doctor = realm.createObject(Doctor.class);
//FULL NAME
FullName fullName = realm.createObject(FullName.class);
fullName.setFirstName("Joe");
fullName.setLastName("Richard");
fullName.setMiddleName("Brown");
doctor.setFullName(fullName);
//CONTACTS
Contacts contacts = realm.createObject(Contacts.class);
String[] phoneNumbers = {"+998903735173"};
contacts.setPhoneNumbers(phoneNumbers);
doctor.setContacts(contacts);
//ORGANIZATION
doctor.setOrganization(organization);

例如,此代码返回所有医生的A类:

RealmQuery<Doctor> query = realm.where(Doctor.class);
RealmResults<Doctor> rDoctors = query.contains("category", "A").findAll();
return rDoctors;

我的app逻辑如下:首先,我打开组织列表。当用户点击一个组织时。这将打开医生名单。

所以我的问题是我可以通过其子对象(组织)找到医生吗?像这样的东西

RealmQuery<Doctor> query = realm.where(Doctor.class);
    RealmResults<Doctor> rDoctors = query.someMagicalMethod("organization", organization1).findAll();
    return rDoctors;

PS。是的,我可以通过深入组织来实现它。我想知道Realm.io是否可以通过对象进行搜索。无论如何我喜欢Realm.io

1 个答案:

答案 0 :(得分:16)

我认为我有可能。您可以在此处查看:http://realm.io/docs/java/latest/#link-queries

根据您的情况,您可以尝试以下代码:

RealmResults<Doctor> rDoctors = realm.where(Doctor.class)
                                .equalsTo("organization.organizationId", organizationId)
                                .findAll();
return rDoctors;

如果它适合您,请告诉我。