Reputation: 112
I have two classes:
public class Employee extends RealmObject {
@PrimaryKey
long id;
String name
Department department;
//getters setters etc.
}
public class Department extends RealmObject {
@PrimaryKey
int id;
String name;
//getters setters etc.
}
It is one to many relationship. One department can have multiple employees. I've tried to make some queries.
realm.where(Employee.class).equalTo("department.name", "deptName").findAll()
realm.where(Department.class).equalTo("employee.name", "empName").findAll()
The first one is working, the second one has an error:
Invalid query: employee does not refer to a class.
How to make the second one work? Do I must make a RealmList of Employees in Department class?
I wanted in this query fetch all Departments which have an Employee with specified name.
Upvotes: 2
Views: 4200
Reputation: 81539
Realm 3.5.0+:
public class Employee extends RealmObject {
@PrimaryKey
long id;
String name
Department department;
//getters setters etc.
}
public class Department extends RealmObject {
@PrimaryKey
int id;
String name;
@LinkingObjects("department")
private final RealmResults<Employee> employees = null;
//getters setters etc.
}
realm.where(Employee.class).equalTo("department.name", "deptName").findAll()
realm.where(Department.class).equalTo("employees.name", "empName").findAll()
Upvotes: 5