A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/realm-sdks/java/latest/io/realm/annotations/LinkingObjects.html below:

LinkingObjects (Realm 10.10.1)

Annotation for defining a reverse relationship from one class to another. This annotation can only be added to a field of the type

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:

Note that when the source of the reverse reference (`dog` in the case above) is a `List`, there is a reverse reference for each forward reference, even if both forward references are to the same object. If the `Person` class above were defined as:

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4