gpt4 book ai didi

Android Realm一对多关系、查询

转载 作者:太空狗 更新时间:2023-10-29 16:27:46 27 4
gpt4 key购买 nike

我有两个类:

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.
}

是一对多的关系。一个部门可以有多个员工。我试图提出一些疑问。

realm.where(Employee.class).equalTo("department.name", "deptName").findAll()
realm.where(Department.class).equalTo("employee.name", "empName").findAll()

第一个运行正常,第二个出错:

无效查询:员工未提及类(class)。

如何使第二个工作?我必须在 Department 类中创建一个 Employees RealmList 吗?

我想在此查询中获取所有具有指定名称的员工的部门。

最佳答案

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

关于Android Realm一对多关系、查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45780968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com