@Retention(value=RUNTIME) @Target(value=FIELD) public @interface LinkingObjects
RealmResults
.
To expose reverse relationships for use, create a declaration as follows:
public class Person extends RealmObject {
String name;
Dog dog; // Normal relation
}
public class Dog extends RealmObject {
// This holds all Person objects with a relation to this Dog object (= linking objects)
\@LinkingObjects("dog")
final RealmResults>Person< owners = null;
}
// Find all Dogs with at least one owner named John
realm.where(Dog.class).equalTo("owners.name", "John").findAll();
In the above example `Person` is related to `Dog` through the field `dog`.
This in turn means that an implicit reverse relationship exists between the class `Dog`
and the class `Person`. This inverse relationship is made public and queryable by the `RealmResults`
field annotated with `@LinkingObject`. This makes it possible to query properties of the dogs owner
without having to manually maintain a "owner" field in the `Dog` class.
Linking objects have the following properties:
In addition, they have the following restrictions:
public class DogLover extends RealmObject {
String name;
List<Dog> dogs = new ArrayList<Dog>;
}
then the following code executes without error
Dog fido = new Dog();
DogLover john = new DogLover()
john.dogs.add(fido);
john.dogs.add(fido);
assert john.dogs.size() == 2;
assert fido.owners.size() == 2;
Querying inverse relationship is like querying any RealmResults
. This means that an inverse relationship
cannot be null
but it can be empty (length is 0). It is possible to query fields in the source class. This is
equivalent to link queries. Please read for more
information.
public abstract String value
IllegalArgumentException
.