Using Parse, you can store data objects establishing relations between them. To model this behavior, any ParseObject can be used as a value in other ParseObject. Internally, the Parse framework will store the referred-to object in just one place, to maintain consistency. That can give you extra power when building and running complex queries. There are three main relation types:
one-to-one, establishing direct relations between two objects and only them;
one-to-many, where one object can be related to many other objects;
many-to-many, which can create many complex relations between many objects.
There are two ways to create a one-to-many relation in Parse:
The first is using the Pointers in Child Class, which is the fastest in creation and query time.
The second is using Arrays of Pointersin Parent Class which can lead to slow query times depending on their size. Because of this performance issue, we will use only pointers examples.
There are three ways to create a many-to-many relation in Parse.
The first is using the Parse Relations, which is the fastest in creation and query time. We will use this in this guide.
The second is using Arrays of Pointers which can lead to slow query times depending on their size.
The third is using JoinTablewhere the idea from classical database. When there is a many-to-nany relation, we combine every objectId or Pointer from both sides together to build a new separate table in which the relationship is tracked.
This tutorial uses a basic app created in Android Studio 4.1.1 with buildToolsVersion=30.0.2 , Compile SDK Version = 30.0.2 and targetSdkVersion 30
At any time, you can access the complete Android Project built with this tutorial at our Github repositories
Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
A device (or virtual device) running Android 4.1 (Jelly Bean) or newer.
Understanding the Book App
The main object class you’ll be using is the Book class, storing each book entry in the registration. Also, these are the other three object classes:
Publisher: book publisher name, one-to-many relation withBook
Genre: book genre, one-to-many relation withBook. Note that for this example we will consider that a book can only have one genre;
Author: book author, many-to-many relation withBook, since a book can have more than one author and an author can have more than one book as well;
A visual representation of these data model:
Let’s get started!
Before next steps, we need to connect Back4App to our application. You should save the appId and clientKey from the Back4App to string.xml file and then init Parse in our App.java or App.kt file.
Follow the New Parse App tutorial if you don’t know how to init Parse to your app.
Or you can download the projects we shared the github links above and edit only the appId and clientKey parts according to you.
1 - Save and list related objects of books
In this step we will see how to save and list the Genres, Publishers and Authors classes related with the Book class.
1.1 - Save and list Genres
We can register aGenreusing the following snippet.
Java
Kotlin
1 private void addGenre(String name){2//We are taking this name parameter from the input.3 progressDialog.show();4 ParseObject parseObject =newParseObject("Genre");5 parseObject.put("name", name);6 parseObject.saveInBackground(e ->{7 progressDialog.dismiss();8if(e ==null){9getGenres();10 inputGenre.setText("");11 Toast.makeText(this,"Genre saved successfully", Toast.LENGTH_SHORT).show();12}else{13 Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();14}15});16}
We can register aGenreusing the following snippet.
In this part, we use a model class named ParseObjectModel. In this model class, we have a ParseObject variable to be able to read the data, and the isChecked variable, which we will use to save the book in the next step. We will be able to easily retrieve the selected objects with the isChecked variable.
Here is the our ParseObjectModel model.
Java
Kotlin
1 public class ParseObjectModel{2 ParseObject object;3 boolean isChecked =false;45 public ParseObjectModel(ParseObject object){6 this.object = object;7}89 public ParseObject getObject(){10return object;11}1213 public ParseObjectModel setObject(ParseObject object){14 this.object = object;15return this;16}1718 public boolean isChecked(){19return isChecked;20}2122 public ParseObjectModel setChecked(boolean checked){23 isChecked = checked;24return this;25}26}
2 - Save a Book object and its relations
2.1 - Save a Book object with 1:N Relationship
This function will create a new Book in Back4app database with 1:N relations.
This function will create a new Book in Back4app database with N:N relations. For the Author relation, we find the selected Author/s in the adapter of the authorRecyclerView and save them as Parse Relation.
Java
Kotlin
1 progressDialog.show();2 book.put("genre", genre);3 book.put("publisher", publisher);4 book.put("title", title);5 book.put("year", year);67//Here we are setting book relation with getSelectedItem function of BookAuthorAdapter.8if(recyclerViewAuthors.getAdapter()!=null){9 relation =((BookAuthorAdapter) recyclerViewAuthors.getAdapter()).getSelectedItems(book);10if(relation ==null){11 Toast.makeText(this,"Please select Author/s", Toast.LENGTH_SHORT).show();12return;13}14}else{15 Toast.makeText(this,"Something went wrong!!", Toast.LENGTH_SHORT).show();16return;17}1819 book.saveInBackground(e ->{20 progressDialog.hide();21if(e ==null){22 Toast.makeText(AddBookActivity.this,"Book saved successfully", Toast.LENGTH_SHORT).show();23startActivity(newIntent(AddBookActivity.this, BookListActivity.class));24finish();25}else{26 Toast.makeText(AddBookActivity.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();27}28});2930//This is the function for save Author/s relation of Book object.This function in BookAuthorAdapter.31 public ParseRelation<ParseObject>getSelectedItems(ParseObject parseObject){32 ParseRelation<ParseObject> relation = parseObject.getRelation("author_relation");33for(ParseObjectModel object : this.list){34if(object.isChecked())35 relation.add(object.getObject());36}37return relation;38}
3 - Query the Book Details with Relations
With these functions, we will list our Books according to their Publishers. First, we throw a query to the Publisher class.
Now, when we click on any Book object, we send the Object Id of this Book with an intent to the page that will show the details of that Book. And we get all the details of the Book from the database by using this Object Id on that page.