1 00:00:00,000 --> 00:00:03,908 [MUSIC] 2 00:00:03,908 --> 00:00:06,461 [SOUND] We've solved the first problem. 3 00:00:06,461 --> 00:00:08,380 Let's move on to the second one. 4 00:00:08,380 --> 00:00:12,110 We need to implement a way for our users to save their pizzas. 5 00:00:12,110 --> 00:00:12,870 To do this, 6 00:00:12,870 --> 00:00:17,755 we'll be using the Room library to store pizza information in a SQLite database. 7 00:00:17,755 --> 00:00:21,605 Room makes maintaining a database a lot easier than it used to be. 8 00:00:21,605 --> 00:00:25,405 Instead of having to write all the SQL yourself, Room gives us a set of 9 00:00:25,405 --> 00:00:28,925 components that handle a lot of that work behind the scenes. 10 00:00:28,925 --> 00:00:31,085 Let's take a minute to look at these components and 11 00:00:31,085 --> 00:00:32,775 see how it all fits together. 12 00:00:32,775 --> 00:00:37,250 The first component of a RoomDatabase is an entity, which represents a table and 13 00:00:37,250 --> 00:00:38,440 the database. 14 00:00:38,440 --> 00:00:44,110 Stepping up from an entity, we have data access objects, also referred to as DAOs. 15 00:00:44,110 --> 00:00:47,600 A data access object is where we'll keep all the queries we need for 16 00:00:47,600 --> 00:00:48,740 a specific table. 17 00:00:49,950 --> 00:00:54,050 At the very top is the RoomDatabase, which groups together all of the DOAs and 18 00:00:54,050 --> 00:00:57,060 entities and provides a single point of entry from within the app. 19 00:00:58,080 --> 00:01:00,940 We'll talk more about each of those in just a bit. 20 00:01:00,940 --> 00:01:03,030 But before we can start on anything, 21 00:01:03,030 --> 00:01:05,770 we'll need to decide on the structure of our database. 22 00:01:05,770 --> 00:01:07,250 What tables does it have? 23 00:01:07,250 --> 00:01:08,712 What columns are in those tables? 24 00:01:08,712 --> 00:01:13,210 And what relationships do we need to maintain between columns? 25 00:01:13,210 --> 00:01:16,970 For us, we'll have a Pizza table and a Toppings table. 26 00:01:16,970 --> 00:01:19,090 Both with id primary keys. 27 00:01:19,090 --> 00:01:21,350 But while this lets us store some information, 28 00:01:21,350 --> 00:01:23,530 it's not giving us the whole story. 29 00:01:23,530 --> 00:01:27,090 We're missing out on which toppings belong on which pizza. 30 00:01:27,090 --> 00:01:29,660 To get this information, we'll need to add a third table 31 00:01:29,660 --> 00:01:33,112 to keep track of all the pizza and topping combinations. 32 00:01:33,112 --> 00:01:36,680 This PizzaTopping table will have two columns which are both 33 00:01:36,680 --> 00:01:38,780 foreign keys to other tables. 34 00:01:38,780 --> 00:01:42,990 And together, those two columns also make up the primary key. 35 00:01:42,990 --> 00:01:47,055 If you'd like to read more about why we need the PizzaTopping table, 36 00:01:47,055 --> 00:01:51,346 check out the article below on how to handle a many-many relationship. 37 00:01:51,346 --> 00:01:54,366 Okay, now that we've got a better idea of what we have to do, 38 00:01:54,366 --> 00:01:58,900 let's start building up from the bottom and get to work on those entities. 39 00:01:58,900 --> 00:02:00,820 Heading back to Android Studio, 40 00:02:00,820 --> 00:02:04,670 I'll clean up my workspace a little bit by closing all of these tabs. 41 00:02:04,670 --> 00:02:08,190 Then the first thing we need to do is add a couple import statements for 42 00:02:08,190 --> 00:02:10,170 the Room library. 43 00:02:10,170 --> 00:02:13,900 Let's head over to our apps build.gradle file and copy and 44 00:02:13,900 --> 00:02:18,164 paste in these two lines to the bottom of the dependencies section. 45 00:02:18,164 --> 00:02:22,280 Also, they've been updating these libraries somewhat frequently. 46 00:02:22,280 --> 00:02:24,370 So if you see a newer version warning, 47 00:02:24,370 --> 00:02:28,250 feel free to use Alt+Enter to upgrade to the newest version. 48 00:02:28,250 --> 00:02:35,050 Then let's sync the project, And now we can go about using our entities. 49 00:02:35,050 --> 00:02:40,060 Let's head over to the Pizza class and add a line above the class declaration. 50 00:02:42,740 --> 00:02:46,890 And then type @Entity and hit Enter to import it. 51 00:02:46,890 --> 00:02:51,090 And there we go, our Pizza class is now a Pizza entity. 52 00:02:51,090 --> 00:02:55,560 However, all entities require a primary key. 53 00:02:55,560 --> 00:02:57,511 So we're not quite done yet. 54 00:02:57,511 --> 00:03:00,463 To tell Room which column is the primary key, 55 00:03:00,463 --> 00:03:04,986 we just need to add the @PrimaryKey annotation before that column. 56 00:03:04,986 --> 00:03:14,440 Let's add a line above id and then type, @PrimaryKey. 57 00:03:14,440 --> 00:03:17,242 You may need to use Alt+Enter to import it. 58 00:03:17,242 --> 00:03:20,190 Awesome, that's it for our Pizza entity. 59 00:03:20,190 --> 00:03:21,836 Now let's update the Topping class. 60 00:03:24,253 --> 00:03:29,217 Let's add the @ entity annotation, and then the @ primary key annotation. 61 00:03:32,711 --> 00:03:37,410 You could also put it on the same line as the id if you prefer. 62 00:03:37,410 --> 00:03:40,300 Now that we've got Pizza and Topping entities, 63 00:03:40,300 --> 00:03:44,870 it's time to create the PizzaTopping entity, which we'll do in the next video.