Hello Everyone,
I hope you are enjoying the byte size Realm Knowledge. This week we will learn some basics on Relationships.
In the Realm Database, A relationship is a connection between two Realm Objects.
In MongoDB Realm, you can define relationships between two objects ONLY within the same partition/realm. Relationships are direct references to other objects in a Realm/Partition.
Please Note: Relationships between objects in different partitions are not permitted. One alternative is to define a manual reference, like a foreign-key-esque concept, store a string in a field that corresponds to a field in another realm, and then query for that field in a different realm.
Relationships are represented in the Schema found in the Realm UI and can be created manually by editing the schema or automatically when using Development Mode.
There are three primary types of relationships between objects:
One-to-One Relationship
As the name implies, A Realm object is related to no more than one other object. Letās take an example of an Author and a Book
Java
public class Book extends RealmObject {
public String name;
public Boolean isRead;
}
public class Author extends RealmObject {
public String name;
public Book book;
}
Javascript
const Book = {
name: "Book",
properties:{
name: "string",
isRead: "Boolean"
},
};
const Author ={
name: "Author",
properties: {
name: "string",
book: "Book"
}
};
Every Author can have zero or one Book instance. Setting the relationship field to null will clear the reference.
One-to-Many Relationship
A one-to-many relationship is when one object relates to multiple objects. In the same above example, an Author can have multiple books. This will be a relationship between a RealmList and a RealmObject.
Java
public class Book extends RealmObject {
public String name;
public Boolean isRead;
}
public class Author extends RealmObject {
public String name;
public RealmList<Book> books;
}
Javascript
const Book = {
name: "Book",
properties:{
name: "string",
isRead: "Boolean"
},
};
const Author ={
name: "Author",
properties: {
name: "string",
books: "Book[]"
}
};
RealmLists can contain multiple RealmObjects of the same or different type (Mixed) depending on how you define it. RealmList can be used to model both one-to-many and many-to-many relationships.
Java
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Author author = realm.createObject(Author.class);
author.name = "JK Rowling";
Book book1 = realm.createObject(Book.class);
book1.name = "Harry Potter and Philosopherās Stone";
book1.isRead = true;
author.books.add(book1);
Book book2 = realm.createObject(Book.class);
book2.name = "Harry Potter and Chamber of Secrets";
book2.isRead = false;
author.books.add(book2);
}
});
Inverse Relationships
Relationship definitions by default are unidirectional. As per the above relationships, you can follow the link from an Author to a Book, but there is no way to go from a Book to its Author object/s.
This is resolved by giving the Book a @LinkingObjects annotation.
@LinkingObjects
This essentially means that for any relationship you have in your schema (either 0ā¦1 or 0ā¦*), you can now see the objects that are linking to your current object.
The Class Model defining an Author with a list of Books
public class Book extends RealmObject {
public String name;
public Boolean isRead;
}
public class Author extends RealmObject {
public String name;
public RealmList<Book> books;
}
You can provide a link in the opposite direction, from Book to Author with the @LinkingObjects annotation like below:
Java
public class Book extends RealmObject {
public String name;
public Boolean isRead;
@LinkingObjects("books")
public final RealmResults<Author> authors = null;
}
public class Author extends RealmObject {
public String name;
public RealmList<Book> books;
}
Javascript
const Book = {
name: "Book",
properties:{
name: "string",
isRead: "Boolean"
authors:{type: 'linkingObjects', objectType:'Author', property: "books"}
},
};
const Author ={
name: "Author",
properties: {
name: "string",
books: "Book[]"
}
};
Please Note: An inverse relationship can only be seen as 0ā¦* meaning it must be declared as RealmResults. Also, when declared, it is final and the accessors are transformed to return the managed linking objects.
Check the Realm SDK documentation to learn more on Relationships.
To Note
-
Relationships are useful for defining a Realm Object Model which is layered with Realm Sync because the schema on the client-side Realm SDK and the server-side MongoDB Atlas must match for Sync to work.
-
Relationships are integral to building a client-side mobile app in the Object-Oriented paradigm.
-
Realm Relationships differ from MongoDBās document model so I will discuss that separately.
In the next weekās session, we will sync our current mobile schema model to MongoDB Atlas.
I hope you enjoyed reading this. I would love to know your experiences working with Relationships, what worked and did not work and how you resolved it.
Cheers