Populating and Relationships15:39 with Craig Dennis
We'll set up a way to populate our database with Courses and Reviews and build the relationships between them.
Amazingly, with just a few new Java classes, 0:00 we've produced a fully functional API. 0:03 But exploring it is a little difficult without any data. 0:06 This is a common problem in all sorts of development. 0:09 You wanna have a database populated with test data so that you can kinda walk 0:12 through the workflow and further walk through relationships. 0:15 We could very easily provide some upload scripts that use SQL 0:19 to populate the database. 0:22 But that kinda messes up our rapid application development flow that we've 0:23 been crafting, just by using entities. 0:26 Now what I mean by that is, if we actually wrote SQL, 0:28 then whenever we change the entities, we also need to fix our SQL scripts to match. 0:31 So what do we do? 0:35 Here comes some good news. 0:37 Spring Boot provides a simple way to do this through the application 0:38 runner interface. 0:41 Here, let me show you and 0:43 then we'll use that data to setup our course to review relationships. 0:44 Okay, so this is again one of those pretty impressive. 0:48 It just works sort of things that Spring Boot brings to the table. 0:52 Now don't blink because you might miss it. 0:55 Okay, so let's make a new class that loads the database. 0:57 We can call it whatever, but let's drop it in the core package, right? 1:00 Let's go in here to the core package and we'll make a new class and 1:05 it's gonna load the database, so what do you say we call it DatabaseLoader. 1:08 Could've been named whatever. 1:12 Okay, and now let's have it implement 1:14 the ApplicationRunner interface. 1:19 Okay, we've got some squiggles. 1:24 Let's go ahead and implement the missing methods, which is just 1, 1:26 which is this run. 1:30 All right, now since we know that we wanna create courses, 1:31 we need to get a handle on the course repository. 1:36 So in order to get that, we're going to use dependency injection. 1:39 That way we can get a handle on however the repository is currently wired 1:43 up, okay? 1:46 So to do that first what you do is you define 1:47 what it is you're trying to get your hands on. 1:50 So we want private, let's set it final. 1:52 We don't need to change this and 1:54 we will say that we're looking for the course repository. 1:55 And let's call it courses. 2:01 Okay, so we have that. 2:05 It's gonna say that it hasn't been initialized which is good. 2:08 We're gonna add a constructor parameter because again we're gonna mark this as 2:10 autowired and what that will do is automatically wire up whatever we have 2:15 labeled as the course repository, nice. 2:20 And now we need to mark this class as a component so 2:24 that it will be found on component standard startup. 2:27 Let's make sure it knows what we're talking about here. 2:36 Here we go. 2:39 And now we get access to a wired up repository that will work with 2:40 all of our entities. 2:44 So let's use it. 2:45 So I'm going to create a course. 2:47 So I say Course course and 2:49 make sure you noticed I'm not gonna make a new one of those. 2:52 And so again we setup a constructor there that takes a title and a URL. 2:55 So let's go ahead and let's do the original course Java basics. 2:59 Remember that and we will set it to https://teamtreehouse.com, 3:04 that's /library/java-basics. 3:11 I am gonna go ahead and 3:16 get us some more real estate, by closing all the tool windows. 3:17 Cool and now we can access the repository that we imported, so 3:23 we'll say courses.save(course), okay. 3:27 And now our server is running, so we've got to remember to restart it. 3:32 So, I'm gonna come down here and I'm gonna click this restart button here. 3:35 Once this gets up and running if we flip over, And we refresh our courses page. 3:40 We should see there it is, our course. 3:47 Let's go ahead and grab the first course here, which is denoted 3:51 under the links tab itself, and this is telling you what the page is. 3:54 Here, so let's go ahead and grab this, grab courses/1. 3:58 Now 1 was the ID that was automatically generated, and 4:02 you'll notice that it's not gonna be in the properties for that. 4:06 We just have title and URL. 4:09 Spring data rest was smart enough and hid the ID field from us. 4:11 Awesome, so now we have that course, 4:15 we know that reviews will work the same way but it's time to make those related. 4:17 Remember, we have one course mini reviews, and vice versa, right? 4:21 So let's model that. 4:25 Now I'm probably not gonna shock you here but it's pretty easy to accomplish. 4:26 So if we go back to our course, And 4:30 we start by making, let's make a new list, all right. 4:35 We will make a new private list of reviews and we'll call it reviews. 4:37 Let's make sure that it knows what we're talking about here. 4:46 Then we are going to mark this as OneToMany. 4:54 And it's going to be mapped by a field named course. 5:00 And the course field is going to be on the reviews and it's not yet. 5:05 So we should just not gonna get ahead of ourselves and go map that. 5:09 So let's go over to the review. 5:11 Well here, in this review, I'm gonna move these guys up a bit. 5:13 And I will add the course field. 5:17 So here we have a private course and it's called course. 5:24 And we want that to be ManyToOne 5:29 on the review, right? 5:34 Cool, so let's go ahead and we can add the Getters and the Setters. 5:40 For that you can Getters and Setters for the course. 5:43 Awesome, and let's flip back now to the course and take a look. 5:48 And we wanna make sure that whenever something is deleted, 5:53 like when this course gets deleted, we wanna delete those reviews. 5:56 So we want the changes to do what's known as cascading. 5:58 So we're gonna cascade. 6:01 And there's different types of cascading but we're just gonna, really everything, 6:03 this is the pair here. 6:06 So CascadeType.ALL, and that will do the proper wiring for us. 6:07 We also wanna make sure that this new list gets initialized. 6:13 So here's an example of where we want to add something in the generic constructor 6:17 and we wanna make this reviews thing initialized. 6:22 Right, so we wanna say, reviews = new ArrayList. 6:24 And we can use the diamond operator there. 6:27 And because course is calling this, 6:31 it's gonna automatically initialize the reviews. 6:33 Nice, so let's do this, let's generate a getter for the review. 6:37 So I'll definitely wanna make sure that people can say, 6:42 hey what are the reviews related to this? 6:44 But what we don't want them to do is we don't want them to set all of the reviews 6:46 because that would be pretty easy to overwrite the review. 6:49 So a common practice is to allow a new method that just adds. 6:51 Okay, so we're gonna say addReview. 6:56 So in here what we can do is we can say review.setCourse(this) and 7:05 we'll set it to this and reviews.add(review). 7:11 Now one thing that we wanna do is make sure 7:15 that we have a constructor for reviews. 7:19 We had not created one of those earlier, we just made the blank one. 7:23 So let's go ahead and do that. 7:26 The way that we're gonna use it originally, 7:28 we're just gonna have the rating in the description. 7:30 Let's keep the course off as just being set the way that we did it. 7:33 If we wanna change this later, we can, we can design this and it in a different way. 7:36 For now let's do it that way. 7:39 So back in our database loader. 7:42 Let's add a review. 7:43 And this is a review that I actually did get on Java basics, if you remember to 7:45 make a little joke or I call myself a dork in it and this is the review that I got. 7:50 It was a 3, not a 1, not bad but it said this, you are a dork. 7:54 I think there are three exclamation points. 8:02 Fair enough, true that. 8:04 Now if we come and restart the server. 8:07 When we refresh the courses here, you'll see that there's now this review's link. 8:14 Cool, so courses/1/reviews, awesome. 8:19 So there's the reviews and it's linking back and it's linking backwards as well 8:26 right so here's the course link and it links back to review course. 8:29 Also, we can look at the review itself. 8:34 So if we go to the reviews/1. 8:36 You'll see that everything is linking the way that it should, right. 8:38 So we can get to the course from the review. 8:42 I wonder what happens if we create a bunch of courses now. 8:46 One way to do that sort of thing is to generate data. 8:50 Now I do this typically so I can visualize what the API will look like. 8:52 Now it's not too hard but please don't let it distract you. 8:56 But what this will do is it will help our fellow teammates building out their 8:59 applications because now they're gonna data to work with it. 9:02 So let's come over here and typically what I do is I create the template. 9:05 That we're looking for, right? 9:10 So we're looking for a course title. 9:12 So it will make an array, string, templates. 9:14 Let's see. 9:19 See what we can come up with here. 9:20 Hello up and running with %s and 9:22 how about we take a look into %s basics. 9:28 These are basically string templates. 9:37 How about %s for beginners. 9:41 How about %s for neckbeards. 9:44 About under the hood. 9:50 Something like that, right. 9:56 All those courses kind of have the same names. 9:57 So little formatting trick there. 10:00 And then let's do this, let's make some buzzwords all right, 10:05 some popular buzzwords. 10:08 Feel free to do whatever you want on here. 10:10 So do an array of buzzwords that we might be having courses of, 10:11 let's do one specifically also. 10:17 Let's do Spring REST Data Java 9. 10:18 Scala, Scala, how are we gonna say that? 10:25 Groovy, let's do hibernate. 10:32 All right, now we could bring in some random numbers but 10:41 let's just loop over these a little bit, right? 10:43 So one way of doing this is using an int stream which is introduced in Java 8. 10:45 It allows you to produce a stream of integers between a range. 10:50 So it's like this. 10:53 So we're using, I'll say IntStream and we will do a range from 0 to 100, okay. 10:55 And then we're gonna say forEach. 11:01 So for each one of those numbers, we'll call it i just because 11:08 you usually do if we're doing a for loop it would look kinda look like that. 11:12 And then we're gonna open up our little lambda here. 11:14 And for each one of those what we wanna do is we want to pull out a template. 11:17 And there's only a certain amount of template. 11:22 So what we can do is we can use a module load to get the next item in the list. 11:26 So we'll say i% which is modulo templates.length. 11:30 And remember that will get us the remainder and will for 11:34 sure be something that's within the range of our list, 11:37 that's kind of a nice trick when you're working through lists like number lists. 11:40 And then let's get a buzzword. 11:44 So this will say, well this isn't a line and 11:44 say String_buzzword = buzzwords. 11:50 And we'll use the same modular trick there. 11:57 % buzzwords.length. 12:00 Finally we'll put them together. 12:07 We'll say title =_String.format. 12:10 And we will give it the template that we created and inject our buzzword. 12:12 And another title will be up and running with spring this data sort of 12:20 thing and next we will make a course. 12:26 So let’s say course, c =, because various course out there, 12:28 new course and going to pass and title and whatever the URL is we could have 12:34 made this example.com, great site for online learning. 12:39 If you now want to add the review so it's a see dot Ad Review and we'll make a new 12:46 and here let's go ahead and the ratings gonna be between one and five. 12:53 And we're going to format the message that comes out here, let's say, 12:59 string format more please. 13:03 Basically, that's what most reviews are, right? 13:09 We all want to learn more, we can't get enough knowledge, so 13:11 we're going to say more buzz word, please. 13:14 Here we go. 13:19 And let's go ahead and let's, outside of this little stream here, 13:20 let's add to an array list. 13:23 So let's say this is a list of courses. 13:27 I'll say this is a bunch of courses. 13:30 So new array list, we'll try to avoid namespace inclusion, 13:33 I realize that these variables are poorly named. 13:38 You can make a review and say that, I did poorly name variables in this one. 13:43 So we're gonna say courses which is are opposed and 13:49 we gonna say save and we gonna pass in this bunch of courses. 13:54 Here we go so we have a bunch of new courses and reviews. 13:58 So now whole bunch of stuff gets generated including the original 14:02 that original courses is still down here and move. 14:07 Let's move him up there with 14:11 radius from what I think confused there we go. 14:16 All right now finally let's restart the server and 14:21 make sure that our work was not all for not come back over. 14:24 Start server up. 14:28 Awesome and if we come back and we look at our courses page, our /courses. 14:34 What happened? 14:43 We forgot to add to the bunchOfCourses. 14:47 Probably sitting there screaming at me, hit the add to the courses list. 14:50 So I'm gonna have courses and we're gonna have that new course that we get 14:54 there we go and then restart the server. 14:58 You put that in the review to say like, 15:04 hey why don't you add that to the course thing too. 15:05 All right, so we’re going to refresh. 15:10 Here we go, look at this. 15:13 Here's our Java nine basics, scallop for beginners, groovy for 15:13 next periods under the hood with Hibernate up and running with Springer. 15:18 That's a lot of data though, isn't it? 15:21 Can you imagine getting that back from an API blast? 15:23 You know what, most websites they kind of do kind of like a paging thing, 15:25 you know like eleven to twenty, here's results twenty one to thirty. 15:29 That's paging, that's called paging. 15:31 Well guess what? 15:33 It comes for free. 15:34 So let's take a quick break, 15:35 and I'll introduce you how to page in spring data rest. 15:36
You need to sign up for Treehouse in order to download course files.Sign up