Live Coding Spring Data Queries to the End of the Persistence Universe

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everybody to spring one i am you know i keep saying i'm so excited but it seems like as the day goes on and on i get even more excited with each additional topic so the next talk that i'm just so looking forward to is live coding spring data queries to the end of the persistent universe and it is brought to us by greg turnquist take it away greg all right thanks i'm glad to be here i'm gonna i'm gonna switch on my slide deck here and for a little bit of a warm up and then uh oh let me hit the share button so we just have a handful of slides and then we're going to jump to the actual live coding now i hope you were able to catch kristoff's presentation earlier about to spring a spring data's guide to the persistence uh and this one we're going to take that stuff and we're going to write it by we're going to do it by uh real live coding and just as a quick intro who am i i'm a spring team member specifically i work on the spring data team i'm an author i've written uh several books on spring boot just go check that out i also run the spring boot learning youtube channel i'm a coffee drinker and i'm a dad dabber i had this theme going and that sounded neat when i wrote it down on slides so you tell me if that's a good joke or not so i hear that you want to write some queries you want to query some data and get an answer to an all-encompassing question i don't know what the answer to life universe and everything maybe not that big so what are you going to need first of all have you seen a spring data's guide to persistence if you caught it this morning awesome if not be sure to check it out on the replay because this presentation in kristoffs is meant to go hand in hand so you can take a peek at that when you get a chance so if you're all prepped up then go and grab your towel because we're going to code enough queries to hit infinite and probability time for some live coding pivot towards sharing just my ide here okay all right now i love to start any presentation with something like let's go to start.spring.io that's how josh does it it's awesome but we actually have a finite amount of time to get to infinite probability so i can't start there so i've actually preempted this and created a nice little nice little set of entity objects we have an employee object here with a name and we also have a manager type sort of a one-to-many relationship here mapped out now if you're wanting to know more of the gut details of how to do jpa stuff then after this talk over on the intermediate advanced talk is something to go into a more deep dive on actually squeezing the everything you can out of jpa i'd go check that out if you want to learn more intimate details about jpa here we're talking about how to query data and just as the as the prep here this is where i have all my little bits of data initialized and stored in an embedded database um here i happen to have a manager created and a couple employees and i have them linked together so one manager has one or more multiple employees now with our little data structure set up we need to kind of hang this stuff if we're going to write queries um first of all we're gonna we're gonna go create something based around the our employee and manager type and then after that we're gonna put an api we're gonna wrap around it now with spring data the idea is to simplify writing queries and get to the business that you're trying to do so so we have here is what's called a repository class and it's actually not a class if you'll notice carefully it's an interface and with this interface here we can actually declare the thing but declare our operations that we want to query with spring data and spring data is going to go right them for us now we're doing is we're extending a pre-baked interface called jpa repository that comes from spring data jpa and all we need to do is tell it what type does this repository extend over in this case our employee type that i showed this that i that i pre-baked for this presentation and what the type of the id is in this case a long type and here i've got an empty this is an empty repository or so it seems if i go over here and start leveraging this thing we're going to see what actually we have hold up so i'm going to add this particular one to the constructor call i'll go ahead and add the other one as well let's start writing our first api so we want to just how about we just start with give me everything so i'm going to do a list of these employee objects just call it all okay i'm going to grab this and that's not what i wanted let me grab the employee repository find all where did that method come from that was an empty repository wasn't it not quite if i open that up if i use my ide to navigate it you'll see that inside that jpa repository that came from spring data jpa there's already some predefined interface operations there's several finders built in save operations down here there's some delete ones and ways to fetch other ways to fetch some basic data so we actually get something for nothing and i'm actually going to write here let's go ahead and get to that first api built repository so i'm launching the app here i happen to have some debugging turned on and when you're writing queries for the first time i highly recommend turning on stuff like spring or gotspringframework.data and you can tell it to show all the sql if you use spring.jpa.show sql this is stuff built into booth that will do the trick for you but now nicely my little ide i can just jump to a terminal here and i can curl that end point now if i just do that it's a little hard to read i like to pipe it through the jq utility to make it print friendly and if i scroll it up here you can see i've already got some data loaded right away we've already written our first query and how much sql do we write to do it zero the sql that you do not write doesn't have any bugs in it okay got that first query done okay now let's go back here and think about what else do we want to fetch you may want to fetch individual ones like maybe you know which one you want to get it's common to somebody provides an id value and they want to get a single employee then again what we get find we have an option here to say find by id we can fetch a single a single one if we know who we're looking for and by the way we have these parameter drives types how am i going to get it to here i'm going to use a little spring nvc magic and just use a path variable annotation now it's red here there's something wrong what is that that's because it's actually configured to return back an optional because we don't know if there's anybody there yet so there's a chance that somebody could be clearing on a non-existent id and there's nothing there so in that case i'm going to put in an or clause or else i'm going to throw an exception there we go now i can just restart all that let's see we got so if i go back to the terminal here i did a little curl operation if you remember i got all the employees so i've got let's look at here here's mr here's the second id this particular user so i should be able to just go put this in here go put slash 2 and get a single user in that case our single single employee record there so already without writing a line of either sql or jpa or whatever query language you're using right now we're using jpa but this also works on mongodb redis whichever data store you're actually using you're actually getting access to data without having written any code at all so we can either right now in our current setup we can either get all the data or we can get none of the data or sorry one item of the data sometimes we need though is somewhere in the middle and so to do that i need to go right let's go write some more detailed queries for example maybe i want to go find an employee based upon a name search let's create a let's call it uh let's do let's do a name match we want to search based on the user's name let's get let's return back a list uh search based on name string name okay in this case i'm gonna i want to get back i want to find by name uh oh we don't we don't have a find by nam operation time to actually write our first custom finder now i'm going to go back over to the repository that we started with and i'm going to start making i'm going to spec it out here i'm going to define that i want to get a list of employees back and if you'll notice here right here as i stop i'm using intellij idea but any modern browser that's got spring support built in can handle this it's actually helping me complete this by showing me what i have access to find by name and i can just do that string name so it's saying it's ready to do a perfect match by parsing this operation and it's going to do an exact match case included on the name field so i should be able to just restart this and get it going okay let's switch over let's do that let's query that endpoint now we have to use an exact match so i'm going to use a percent 20 to represent to do a space properly if i do that i can get a particular operator or maybe i can get back here and look for somebody else but who wants to escape right escape operations like that left and right why don't we make it simpler and instead just do a partial match how about that so i can go back here and define a more detailed one and here again if i use the ide can help me do a match here so if i do string i can do partial match containing in fact let's add a little bit find by name here let's do let's do that one so so maybe we're going to call this partial match let's make our let's make our api operation a little smoother find by name containing partial match so it'll it'll put checks on each end all right oop i did not mean to hit the debugger let's uh turn that off okay okay let's uh let's just restart that one for good measure make sure you don't have any conflicts going on here okay so let's just say i want to search on baggins what do we got here right away we have frodo baggins and bilbo baggins so we have matches based on that we could search on search on frodo and get just that entry so now what happens if i use lowercase is it gonna is it gonna work is it gonna work well it worked it just came back empty because it was still checking on the case sensitivity so let's see find by name containing see ignore case and let's restart that now what happens if i try to search on that lowercase photo what happens when it's going to get me a match so now in a in in a short span of time we're able to implement the search function that uh your manager came in and said we need the search box we need to be able to put it up there so people can do partial matches and things like that so we can do operations we've got to be able to do that and something they talked about in the presentation was we need we need the stuff to flow we need the queries to flow and we need to be able to keep you in the developer space and in this way we can do that because you're not wasting your time gluing tables together if you will instead you're able to focus on what do i need to write the code that solves this problem so let's go write something even more detailed now we know that a given employee has a manager what if you're trying to find i need to find all the employees based on who their manager is like you're trying to look up a department and so you know instead of dealing with clearing against a single table you're trying to match based on a manager name search based on manager manager name now we're going to do that well we can go over here to our repository basically these queries let you actually navigate across relationships and so for this kind of thing i'm going to i'm going to create a find by manager operation find by manager name string name so when you have an operation like this it will look at what what's the manager attribute of the employee record it'll see that that's a relationship it'll cross over to the other side if you will to the manager object and then let us match on the manager attribute manager.name and over here i can then find by manager name and if i restart that i can see that what that gives me i can spell the name gandalf please about a manager name but i must type something what it blew up on me proof that it's not proof that this is a live demo oh manage name is not aha there we go all right let's kick that one out of the ditch and get it going so here let's try to go cross a relationship a one-to-many relationship between manager and employee and see if we can find all the employees tied to this particular manager and here you go frodo baggins and bilbo baggins and if i if i check against our other manager in the system sarah man man okay we got our other employee and let's see what those flags flipped on that i just showed you earlier really makes it easy i'm in the wrong terminal it makes it easy to go to bug stuff because then you can go see exactly what queries are being written to go fetch data in hibernate now the last one let me see if i can fit in here i'm trying to fit in as many as we can do here there's another service that that we have for example if you're building a search box or like a filter on a website and you want the you want the user to define everything that they're getting okay just call this a general search and you want to return back a list of whatever your type is what you want is what's called an example let me show you what i'm talking about here so somebody feeds in they can pick any criteria request param uh maybe they want to pick the name maybe not um maybe they want to pick the role or maybe they want to pick let's call it manager name since we've been doing that one oh let's call it manager name and i'm setting these by default these parameters are required but i'm setting them to be optional manager name so what i'm going to do is i'm going to go create an employee object here right on the spot and i'm going to assign it these values that i came they could be null they could have been populated with values let me just check make sure that's uh okay i need to do first there we go we'll do that okay so i'm going to populate this this object here it has the name has the manager some of those fields could be null some of them may not be and i'm going to create what's called an example object and i'm going to then [Music] find all where's the here by example find all based on this probing example let's restart this and we'll see what this lets us do so here's where you're pushing it off to the user to give them the option to do that what are they going to pick to do so if i go to the terminal i can do this let's say name equals baggins okay that doesn't match unless i do that encoding stuff okay so if i do a match on bilbo baggins i get a hit right okay or if i do the bilbo or if i do frodo but i can also change up what i'm doing and this lets me add other criteria without even having to go write another finder so if i use manager name equals gandalf it finds both of these two now there's other there's options to extend this for example right now the query by example option it's based on what values are not null but you can add extra criteria like starts with ends with ignoring case i don't think we have time to go into that stuff i'm trying to keep an eye on the clock here and see if we can fit in one more bit of uh good stuff here let's see if we can do a little auditing so first of all i'm going to turn it i'm going to turn on jpa auditing we get asked about this uh fairly frequently about what support we have for being able to get keep track of stuff so i'm gonna register an entity listener for my employee object let's call it the auditing uh entity listener and this is going to let me go in here and say create a date i'm going to put i'm going to pick a javautil.date and let's go go add a getter for that let's see i think that's everything that we need in order to demonstrate that so let's restart the app let's go query go run one of our recent queries and you'll notice we have a new field down here we have a create date uh it's possible to register other stuff like who who did the creation there's also you can do updates so you can track a lot of stuff directly in the database as to what operations happen and it really adds a lot of power to all that what i'm going to do is i'm going to switch back to my deck because i just have a few wrap-up slides here let me go go back to keynote i remember that uh that thing about get back getting back into the flow and so with all this kind of stuff i say the left the queries must flow and then you're you're supposed to say well hold on that that what do you what are you doing i thought we're on the hitchhiker's theme here well i i like science fiction i like fantasy and i like comedy so little hitchhikers plus plus lord of the rings thrown in with a little dune why not so let's um i want you to be sure to stay connected if you want to get more of this content some of my live streams and stuff go visit my youtube channel at spring boot learning and also don't forget to go catch uh kristoff's talk a spring data's guide to persistence on the replay when it's posted on september 7th and then after this talk if you if you want to see more core detailed deep dive stuff on jpa go over to the intermediate advanced track for the next talk that's coming up right after this one thanks everybody hello everybody greg oh my gosh thank you so much now i not only want to write more queries using jpa and what's offered by spring data now i want to go watch dune go back and re-watch a hitchhiker's guide and read the book so you have given me so much material to to explore so thank you so much for that presentation folks we are now oh folks i want you to join um greg now in the uh zoom chat that zoom chat is a link should be in your slack room so go in there click it feel free to ask greg questions about the presentation or anything that you need to want to explore about the queries and up next is going to be an exciting talk called beautiful spring tooling for the masses we have hottie martin and sandra going to be doing that presentation and that will start at 1205 pm eastern i look forward to seeing you then take care you
Info
Channel: SpringDeveloper
Views: 773
Rating: 4.7142859 out of 5
Keywords: Data/Databases
Id: qYDr8LXyDTY
Channel Id: undefined
Length: 26min 11sec (1571 seconds)
Published: Wed Sep 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.