Entities and Repositories Review9:28 with Craig Dennis
We'll discuss packaging styles and build out our model entities and matching repositories.
So let's get this party started. 0:00 So how should we get started? 0:03 Well we know what it is that we're trying to track. 0:05 So why don't we build our model first? 0:08 More specifically, we can build our course and review entities and repositories. 0:09 Now before we jump in and start coding these up, 0:15 I wanted to talk a wee bit about style. 0:17 One of the things that we try to hammer home here in our content is this. 0:19 There are different tastes and styles when you work collaboratively on a project. 0:22 Now you as a developer are going to be dropped into many projects throughout 0:27 your career and they're going to follow not only different coding styles, but 0:30 also different folder structures. 0:33 In an effort to simulate these differences I thought I'd talk you through 0:35 the popular ways that you might find projects packaged. 0:38 A decision that happens at the very start of each project is how to 0:41 organize your packages. 0:44 So far in most of our projects, we followed the package by layer approach. 0:46 The concept here is that the application layer is exposed and 0:51 each directory there represents an implementation category. 0:54 This is a common layout for when you understand the application structure of 0:57 what is being built from a technical level. 1:01 The downside of this approach is that you have no idea what the application is all 1:04 about, just from looking at it at the package level of things. 1:08 Let's contrast that with the Package By Feature style. 1:11 So, see here in the Package By Feature style of things, we can see 1:14 all the features and it'd be clear for someone maintaining our code base, 1:17 where to start looking for things revolving around reviews. 1:20 The same file still exists, they're just categorized differently. 1:23 You'll see both styles alive and very active and 1:26 as you've heard us say repeatedly, 1:29 you should always go with whatever style is in place in your existing code base. 1:31 If, however, your project is greenfield or in the starting from scratch portion, 1:35 by all means, this is a decision that should be made together with your team. 1:39 I've got more information and teachers notes, check it out. 1:43 You're probably most familiar with the Package By Layer approach 1:47 as it makes the most sense when we're teaching each of these application layers. 1:49 But let's do something kooky. 1:53 Since we're just starting out with this project, 1:54 let's embrace the Packaged By Feature approach for this project. 1:56 I talked with out international agency team, 1:59 and they agreed that all the code we produce will follow this packaging. 2:01 That was easy. 2:05 Let's go build those packages. 2:06 Okay, so package names are singular right? 2:08 And our first feature that we would create was one for courses. 2:11 So what we should do is we should make a new Java file and 2:14 we will put that in com.teamtreehouse, the root and the package is going 2:19 to be called course because it's singular and the class will be called Course. 2:25 And we'll click OK. 2:30 And let's mark our new class here as an entity. 2:31 So let's see what does a Course have. 2:37 It has a title, private_String title. 2:38 And it also has a URL, private_String_url. 2:45 Now, JPA, the Java Persistence API, 2:49 requires the constructor that takes no parameters. 2:51 So let's go ahead and let's make that protected. 2:55 All right, let's do a protected constructor that takes no parameters and 2:58 then, that way, only classes in this package can access it. 3:03 What we'll do later is we'll make a friendly public constructor. 3:06 We don't really want to expose this. 3:08 You know what, 3:09 speaking of JPA, we're gonna need to make sure that we have an ID field defined. 3:10 We're probably gonna have a lot of these right? 3:15 So let's make a long private final Long_id. 3:16 We don't ever want anybody changing this. 3:22 Okay, so this is going to complain. 3:24 Let's go ahead and initialize it in the constructor. 3:26 Squiggle be gone. 3:31 Now the reason why we don't wanna set this is because our database is going to be 3:33 responsible for this id field, the way that it will uniquely identify the entity. 3:36 So, in fact, we need to set that up with some annotations, so let's go ahead. 3:42 And first, we know that it's an ID field. 3:45 And we also want it to generate that for us. 3:47 We don't wanna do that ourselves, so we'll say this is a generated value and 3:50 the strategy that it's going to use is called GenerationType.AUTO. 3:56 And we'll just go ahead and add those in there. 4:02 Now obviously we're gonna want to have some getters and setters for 4:07 our field, so let's go ahead and let's generate them. 4:12 So let's not put the ID field out just yet, let's put out the title and the URL. 4:16 We should probably also make the constructor that we talked about 4:21 earlier, right? 4:24 So that people can easily create these. 4:25 So let's do that. 4:26 So let's go and we'll generate a new constructor, and again, ID's really not 4:29 any other business, so let's just go ahead and generate a title and a URL. 4:33 Now, hm, we've already defined what happens in our no argument constructor for 4:38 id and we really just want to run the code in that constructor, right? 4:42 And this one here, we know that we want to null it out. 4:46 Again, we got our red squiggly back. 4:51 So what we can do, is we can actually call that constructor by doing this. 4:52 So the first line here, if we say this, what that will do is it will call, 4:58 in this context, call that id and set it to null. 5:02 All, right, so that's looking pretty good for us now entity wise. 5:05 Let's kick out a repository really quick. 5:08 Okay, so the course repository will live in the same course package and 5:10 we'll just add a new interface. 5:15 So we'll say new Java class, 5:17 we're going to flip this to interface called course repository and 5:19 it's going to drop it right in that course directory there. 5:24 Cool and we're gonna make it extends_CrudRepository. 5:29 And the CrudRepository accepts generic so 5:37 let's parameterize that type saying course and the ID type is Long. 5:40 So remember, this will inject sensical implementations for Crud automatically. 5:46 So that's create, read, update and delete methods are all built for 5:52 us, automatically. 5:56 Pretty cool, right? 5:57 Wait until you see what it does here in a bit. 5:59 All right, so let's go build a review entity and repository. 6:01 So that should be in a new package, right? 6:05 So we're gonna come over to Java and we'll say New>Java Class and 6:07 we're gonna say com.teamtreehouse. 6:12 We'll make the new package called review singular and we'll make a new Review. 6:15 And this review is going to be an entity. 6:18 Let's see, reviews have a rating, right? 6:26 So integer rating and they also have, a description. 6:30 Okay, so, let's go ahead and knock out those Getters and Setters. 6:42 Cool okay and now let's add the ID. 6:49 Now, let's do just like we did with the Course entity. 6:53 You know when you say that to yourself, 6:57 you should ask hm, is this common functionality? 6:58 It is, isn't it? 7:02 Let's go ahead and let's just copy and 7:03 paste what we have in this course thing and feels like a little weird, doesn't it? 7:05 Let's go over here and let's copy this and let's paste it. 7:11 It is okay to import all that stuff. 7:14 It's complaining the same way that the other one complained, isn't it? 7:17 Okay, so let's go ahead and let's add a new constructor. 7:21 We'll make it protected, protected Review and 7:24 again let's set the id = null. 7:31 And now the JPA will be happy just like before. 7:36 Okay, I'm gonna add a comma here cuz [LAUGH] man 7:40 man my spidey sense is tingling. 7:43 So let's do this, let's say TODO:csd. 7:45 We are duplicating code here for 7:48 every single entity. 7:53 Share it and we'll get back to later. 7:58 Okay, so let's hold off for a second before setting up our relationships here 8:01 between the course and review. 8:05 First let's build out our review repository, right? 8:07 So let's come over here and we'll say New> Java Class, 8:11 and this is a ReviewRepository. 8:16 And again those things are interfaces, and 8:19 it's going to extend the CrudRepository and 8:25 it is of reviews and the ID type is long. 8:31 All right, nice. 8:36 All right, so we got our entities and repositories built and 8:39 pretty amazingly, thanks to the magic of spring data, they're all wired up. 8:42 We'll take them for a test drive here in a bit. 8:46 You really should make sure that we set up those relationships that we talked about. 8:49 Remember the one course, the many reviews and vice versa. 8:52 But that can wait for a bit. 8:55 Nice job adapting to the different packaging style. 8:57 Do you think it makes sense? 8:59 Now that we've got something sorta built, imagine hiring a new teammate and 9:01 having them explore our code base. 9:05 They'd know right off the bat that this project has something to do with courses 9:07 and reviews, which is totally true. 9:10 All right, now, 9:13 why don't we take care of that code duplication to-do that I dropped in there. 9:14 Let's see if we can't get a parent class that encapsulates that idea and 9:17 constructor mass for us. 9:21 I mean, every entity we add to this project is going to be the same. 9:22 Why repeat ourselves? 9:25 See you in a bit. 9:27
You need to sign up for Treehouse in order to download course files.Sign up