Chapters

Hide chapters

Saving Data on Android

Second Edition · Android 11 · Kotlin 1.5 · Android Studio 4.2

Using Firebase

Section 3: 11 chapters
Show chapters Hide chapters

8. Mastering Relations
Written by Subhrajyoti Sen

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you learned all you need to know about tables, entities and annotations. You also learned how to create your database and how to get a runtime instance of it by using Room.databaseBuilder().

In this chapter, you’ll learn even more about entities by creating relations between them using foreign keys and @Relation. Along the way, you’ll learn:

  • How to create a relationship using primary keys and foreign keys.
  • How to define a one-to-many relationship in Room.
  • How to represent different kinds of relationships using entity-relationship diagrams.
  • How to use @Embedded.
  • How to use @ForeignKey.
  • How to use @Relationship.

Note: This chapter assumes you have basic knowledge of Kotlin and Android. If you’re new to Android, check out our Android tutorials. If you know Android but are unfamiliar with Kotlin, take a look at Kotlin For Android: An Introduction.

Getting started

If you are following along with your own app, open it up. If not, don’t worry. You can use the starter project for this chapter, which you can find in the attachments. Now, open the app in Android Studio 4.2.1 or greater by going to File ▸ Open, and selecting the starter project directory.

Once the starter project finishes loading and building, run the app on a device or emulator.

The DroidQuiz Application.
The DroidQuiz Application.

Great! The app is working as expected.

The code is basically the same as the previous chapter. But, if you are just getting started, here is a quick recap of the packages and the code:

  • The data package contains the db and model packages. The db package contains the class that creates your Room database, while model contains all of the code for the entities created in the previous chapter.
  • The view package contains the code for all of the activities of your app.

You’re no doubt eager to start writing some code. But, before that, you’ll need to learn a bit of theory first!

Relations and entity-relationship diagrams

In this chapter, you’ll create a relation between two entities that you created in the previous chapter — Question and Answer. The only problem is that relationships are always hard to understand even if it’s just between two single tables. Therefore, in this section, we are going to talk about a little tool that will help you to better understand the different kinds of relations between — entity-relationship diagrams.

Entity

Represents a component, object or a concept of a system. Concepts described by an entity can be concrete, such as a student or a car, or abstract, such as an event or a schedule. Entities are translated as tables when creating your database schema. They are commonly illustrated as rectangles in most ER diagrams, as shown on the Image below.

The Student Entity.
Kho Kdagobm Alqich.

The User Entity.
Gra Axov Ekmusv.

Relationship

A relationship tells you how two entities interact with each other and it’s usually represented as a verb surrounded by a diamond. For example, think about a student entity and a class entity. Their relationship could be described as follows:

Relation between Entities.
Kirapeaj ximjuac Obkifoaz.

Cardinality

Last but not least, cardinality tells you the kind of relationship two entities have. There are three main cardinal relationships:

One-to-One Relation.
Ife-sa-Ari Wepocuic.

One-to-Many Relation.
Egu-bu-Mitp Docagaus.

Many-to-Many Relation.
Fagt-du-Vulj Nexuciot.

ER Cardinalities.
AT Hemjifidaseuv.

Creating your relations

As briefly mentioned in the previous chapter, you’ll only need to create one relationship between the entities in your app, and its ER diagram looks like this:

One-to-Many relation between Question and Answers.
Ebo-yo-Habb vinupaap nijraan Huuzpoal asb Alqnizv.

Defining a foreign key

Open Answer.kt under the data ▸ model package.

@Entity(tableName = "answer",
  foreignKeys = [			// 1
    ForeignKey(entity = Question::class,			// 2
      parentColumns = ["question_id"],			// 3
      childColumns = ["question_id"],			// 4
      onDelete = CASCADE)			// 5
  ])
import androidx.room.ForeignKey
import androidx.room.ForeignKey.CASCADE

Defining an index

One important thing to remember is that, if you define a foreign key constraint, SQLite requires that you create a unique index in the parent entity for the mapped columns. It’s also recommended in the documentation that you create an index on the child table to avoid full table scans when the parent table is updated. If you don’t, Room will throw a compile time warning.

@Entity(tableName = "answer",
  foreignKeys = [
    ForeignKey(entity = Question::class,
      parentColumns = ["question_id"],
      childColumns = ["question_id"],
      onDelete = CASCADE)
  ],
  indices = [Index("question_id")])		// only this line changes
@Entity(tableName = "question", indices = [Index("question_id")])

Using @Relation

Now, say you want to retrieve a list of all the questions with their respective answers. To do this, you would need to write two different queries: One to retrieve the list of all the questions and another to retrieve the answers based on the question_id value. Your DAO would look like this:

@Query("SELECT * FROM question ORDER BY question_id")
fun getAllQuestions(): LiveData<List<Question>>

@Query("SELECT * FROM answer WHERE question_id = :questionId")
fun getAnswersForQuestion(questionId: Int): List<Answer>
@Embedded		// 1
var question: Question? = null

@Relation(
  parentColumn = "question_id",		// 2
  entityColumn = "question_id"
)
var answers: List<Answer> = ArrayList()		// 3
The final DroidQuiz application.
Pfu yohim YwuuhGoav endjofeyiid.

Key points

  • An entity relation diagram, ER diagram or ERD is a kind of flowchart that illustrates the relation between the components of a system.
  • Entities represent a component, object or concept of a system. They are usually translated as tables in your database.
  • Entities in the Crow’s Foot notation also include a list of attributes or properties that define them.
  • An attribute that can uniquely identify a record of your entity is known as a key attribute and they usually become primary keys in your database.
  • A relationship tells you how two entities interact with each other and it is usually represented as a verb.
  • The cardinality of an ERD tells you the kind of relationship that two entities have.
  • One-to-one relation: When one entity can only be related to one and only one instance of the other entity.
  • One-to-many relation: When one entity can be related to many instances of another entity.
  • Many-to-many relation: When many instances of an entity can also be related to many instances of another entity.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now