java - 如何在 Realm 数据库中创建或实现自关系?

标签 java realm

我有一个名为 User 的表。用户有 friend 。

public class User extends RealmObject {
    @PrimaryKey
    private long id;
    private String name;
    private int age;
    private String email; 
}

我正在使用 User 类将 User 及其 friend 添加到 Realm 。我的问题是,如何将用户链接到其 friend ?

最佳答案

我刚刚浏览了 Realm 文档并在那里找到了我的问题答案。

It is possible to declare recursive relationships which can be useful when >modelling certain types of data.

public class Person extends RealmObject {
    private String name;
    private RealmList<Person> friends;
    // Other fields…
}

Use recursive relationships with care as Realm does not currently have cycle detection and you can easily end in infinite loops.

Setting the value to null for a RealmList field will clear the list. That is, the list will be empty (length zero), but no objects have been deleted. The getter for a RealmList will never return null. The returned object is always a list but the length might be zero.

关于java - 如何在 Realm 数据库中创建或实现自关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35577231/

相关文章:

java - 在 Play 框架中填充下拉列表

ios - 如何在 Realm Swift 上查询 List<Int>

java - 未检查的分配 RealmResults

android - 切换到 Realm 88.2 后的弃用警告

ios - 在 Realm 迁移期间创建 Realm 对象

java - Android Realm 子类实例方法

java - UnsatisfiedLinkError :/tmp/snappy-1. 1.4-libsnappyjava.so 加载共享库 ld-linux-x86-64.so.2 时出错:没有这样的文件或目录

java - Android:java.lang.IllegalStateException:适配器的内容已更改但ListView未收到通知

java - 将带有空格的文件中的二进制数转换为十进制数

Java 9 弃用 SHA1 证书,还是其他问题?