From the course: Building an Android App with Jetpack Libraries
Turn a data class into a Room entity - Android Tutorial
From the course: Building an Android App with Jetpack Libraries
Turn a data class into a Room entity
- [Instructor] My application's user interface is mostly finished and now I'll set up the underlying database. I'll be using the room library to define my database structure. A room-based SQLite database needs three different kinds of classes. The first is called the entity class. An entity, which is also known as a data class in Kotlin, can be used to represent a particular database table. It looks just like a standard POJO, or a plain old Java object class that's used for representing data, but it has some room library annotations. First I need to add dependencies to my project, so I'll go to build.gradle for my app module, and then I'll go over to the room persistence library documentation where the required dependencies are noted. I'll start at the definition of the version and I'll select all the way down here, so I'm including the Kotlin extensions and co-routine support. I'll copy that code. I'll come back to by build.gradle file and scroll down, and then I'll paste that in right here, and I'm going to clean up this code a little bit. I'll get rid of this particular comment and this one too, and then because I'm using Kotlin, I'll change annotation processor to KAPT, K-A-P-T. I also have to add Kotlin KAPT up here, and then I'll resync, and now I'm ready to use the room database library. Now, I'll go to my note entity class. This class was designed to represent data in memory, but it can be repurposed to serve as a room entity class, and to do that, just add an annotation of entity right here. If I leave the annotation in its simplest form like this, I'll be creating a database table named Note Entity, but I can change the name of the table like this. I'll add a table name attribute and set the name of the table to Notes. Next, I need to declare a primary key. The primary key is the data object's unique identifier, and it'll be translated into a primary key column in the database table. I'll place this annotation above the ID property, and then I'll add an attribute of autogenerate set to true, and that means that for each new road that's added to the database, SQLite will generate a numeric value that's unique and it'll just be the next one that's available. There are other annotations available in the room library, including the ignore annotation you can use to prevent a Kotlin class property from being turned into a database column, but for this simple app, this is all I need. I've defined the structure of the app's one and only database table, Notes. The next step is to define the database operations that I'll run with this table to create and retrieve data, and I'll do that next.
Contents
-
-
-
-
-
-
Turn a data class into a Room entity3m 3s
-
Implement database operations in a DAO4m 7s
-
Create a custom type converter2m 14s
-
Define an SQLite database with Room4m 31s
-
Create a unit test for database logic5m 11s
-
Challenge: Add another unit test for database logic1m 27s
-
Solution: Add another unit test for database logic3m 3s
-
-
-
-