1 00:00:00,350 --> 00:00:02,480 We're almost done creating our entities. 2 00:00:02,480 --> 00:00:05,990 All we've got left is one combining pizzas with toppings. 3 00:00:05,990 --> 00:00:09,400 The first step to creating an entity is to create a class. 4 00:00:09,400 --> 00:00:13,810 To get started, let's create a new pizza topping class inside of our data package. 5 00:00:15,090 --> 00:00:16,567 And make sure to pick Class. 6 00:00:18,173 --> 00:00:22,450 Then, just like our other entities, let's make this a data class. 7 00:00:23,900 --> 00:00:26,810 And declare our columns, a parameters to the constructor. 8 00:00:28,420 --> 00:00:31,315 So instead of brackets, we want parentheses. 9 00:00:33,804 --> 00:00:38,316 And inside the parentheses, let's use a val to declare 10 00:00:38,316 --> 00:00:43,510 pizzaId as the first parameter, and make it an Int. 11 00:00:43,510 --> 00:00:46,780 Then let's use another val, to declare toppingId 12 00:00:49,200 --> 00:00:53,930 as the second parameter, and let's make it an Int as well. 13 00:00:53,930 --> 00:00:57,392 Great, now let's add the @Entity annotation above the class. 14 00:00:59,693 --> 00:01:03,060 And all that's left is to declare the relationships. 15 00:01:03,060 --> 00:01:07,780 We'll need to specify the primary key, as well as both foreign keys. 16 00:01:07,780 --> 00:01:09,250 Let's start with the primary key. 17 00:01:10,350 --> 00:01:11,540 In the previous video, 18 00:01:11,540 --> 00:01:16,650 we declared a primary key by using the @primarykey annotation. 19 00:01:16,650 --> 00:01:20,660 However, this doesn't work if your table has more than one column in its 20 00:01:20,660 --> 00:01:22,240 primary key. 21 00:01:22,240 --> 00:01:25,080 For situations like this you have to declare 22 00:01:25,080 --> 00:01:27,391 the primary key up with the entity. 23 00:01:27,391 --> 00:01:30,363 Let's add parentheses after the word entity, and 24 00:01:30,363 --> 00:01:32,651 let me just quickly hide the side pane. 25 00:01:36,140 --> 00:01:38,820 And then let's look at some of the options we can declare. 26 00:01:39,890 --> 00:01:40,830 By default, 27 00:01:40,830 --> 00:01:46,080 each table in the database will be named exactly like its corresponding entity. 28 00:01:46,080 --> 00:01:49,170 However, if you'd like the table to have a different name, 29 00:01:49,170 --> 00:01:52,820 you can do that by specifying the table name property. 30 00:01:52,820 --> 00:01:56,570 In addition to the table name, it looks like there's also entries to let us 31 00:01:56,570 --> 00:02:02,600 specify multiple primaryKeys, and multiple foreignKeys. 32 00:02:02,600 --> 00:02:09,022 Let's set up the primaryKeys by typing, 33 00:02:09,022 --> 00:02:13,070 primaryKeys = arrayOf. 34 00:02:13,070 --> 00:02:16,467 And inside the array, we just need to add the names of the columns. 35 00:02:18,431 --> 00:02:19,513 PizzaId. 36 00:02:22,928 --> 00:02:25,712 And toppingId. 37 00:02:25,712 --> 00:02:31,465 Also inside annotations, and only inside of annotations, 38 00:02:31,465 --> 00:02:36,346 we're allowed to declare a raise using brackets. 39 00:02:36,346 --> 00:02:41,750 Let's use alt+enter on arrayOf to replace this call 40 00:02:41,750 --> 00:02:47,750 with an array literal, which are the brackets, awesome. 41 00:02:47,750 --> 00:02:51,492 Now, to add a comma at the end of our array. 42 00:02:51,492 --> 00:02:55,007 And then add a new line for setting up our foreign keys. 43 00:02:55,007 --> 00:03:01,982 Let's type foreignKeys=, and remember it needs an array of foreign key objects. 44 00:03:01,982 --> 00:03:08,142 Let's add the brackets for the array, and then create a new foreign key by typing, 45 00:03:08,142 --> 00:03:13,172 [(ForeignKey)], and hitting enter to accept auto complete. 46 00:03:13,172 --> 00:03:17,280 Which for some reason, gives us an extra couple of parentheses. 47 00:03:18,350 --> 00:03:19,333 Let's get rid of those. 48 00:03:23,981 --> 00:03:26,580 And then look at which parameters we need to provide. 49 00:03:30,121 --> 00:03:32,900 We’ll need to specify the first three. 50 00:03:32,900 --> 00:03:36,630 The entity the foreign key represents, the name of the column and 51 00:03:36,630 --> 00:03:40,280 the parent table, and the name of the column and this table. 52 00:03:40,280 --> 00:03:42,890 Referred to as the child columns. 53 00:03:42,890 --> 00:03:48,110 Also, even though in this case we're only matching one column with one other column, 54 00:03:48,110 --> 00:03:51,920 it's possible to have a foreign key involving multiple columns. 55 00:03:51,920 --> 00:03:54,140 So instead of just using a string, 56 00:03:54,140 --> 00:03:59,010 we'll need to specify each of these columns as an array containing a string. 57 00:03:59,010 --> 00:04:02,090 Okay, let's start with the pizza foreignKey. 58 00:04:02,090 --> 00:04:04,730 Its entity is the pizza class. 59 00:04:04,730 --> 00:04:13,250 So, let's set entity = pizza:: class, 60 00:04:13,250 --> 00:04:20,400 parentColumns = to an empty array. 61 00:04:22,190 --> 00:04:24,770 The parent column is just the name of the column that 62 00:04:24,770 --> 00:04:26,750 the foreign key is referencing. 63 00:04:26,750 --> 00:04:31,760 Since the pizzaId column corresponds to the id column in the Pizza table, 64 00:04:31,760 --> 00:04:33,880 that's the one we're looking for. 65 00:04:33,880 --> 00:04:38,387 Let's add id to our parentColumns array, and then move on to child columns. 66 00:04:40,062 --> 00:04:42,950 Which is just our pizzaId column. 67 00:04:42,950 --> 00:04:46,580 So let's add a comma, and then set child columns 68 00:04:47,920 --> 00:04:53,350 equal to an array containing pizzaId as a string. 69 00:04:53,350 --> 00:04:57,110 All right, all we've got left is to specify our foreignKey 70 00:04:57,110 --> 00:04:58,670 to the topping table. 71 00:04:58,670 --> 00:05:01,675 I'm gonna add a bit of spacing to make it easier to see everything. 72 00:05:08,603 --> 00:05:12,675 Then let's copy our pizza foreignKey, 73 00:05:12,675 --> 00:05:20,270 add a comma to the end of the line, and then paste it on the next line. 74 00:05:20,270 --> 00:05:25,261 Now we've just gotta change pizza to topping, 75 00:05:25,261 --> 00:05:30,700 and a pizzaId, to toppingId, and there we go. 76 00:05:30,700 --> 00:05:32,860 The pizza topping table is finished. 77 00:05:34,340 --> 00:05:35,540 Pretty cool, right? 78 00:05:35,540 --> 00:05:39,700 We've created a bunch of tables, but we haven't had to right any SQL. 79 00:05:39,700 --> 00:05:43,060 Thinking back to the last video, once we've created the end of these, 80 00:05:43,060 --> 00:05:44,950 it's time to create the dows. 81 00:05:44,950 --> 00:05:47,830 Coming up, we'll take a look at how to query our tables 82 00:05:47,830 --> 00:05:49,670 by using data access objects.