Spring Data JPA -Criteria Queries - The Full Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
websites like Amazon they provide a really really rich search engine and search Pages for example if you want to search for product on Amazon you can provide and add a lot of search criteria for example you can search by name by color by description category Center and so on so forth but did you ever wonder or ask the question how this is implemented in the background or in behind the scenes in this video I will teach you how we can use the criteria queries using spring data jpa and we will we will see together and they will teach you how to implement advanced search queries because we all know the limits of the GPA or GPA repositories work with repositories or even writing SQL queries or raw queries sometimes or most of the times for these cases or for the search cases they do not respond or they do not give us the best experience or the best way to implement such queries but criteria queries or the criteria Builders they provide us with the necessary tools and functions to do so this is what we will learn in this video my name is Ali I'm a senior software engineer and I do videos every week so if you're new to my channel just go ahead and subscribe and enable the notifications to get notified every time I publish a new video if you like my content then just take one second only one second to smash the like button and give me a thumbs up so I can keep continue on producing content like this for you guys also go ahead and join the best developers community that we are building right now I will leave you all the descriptions and all the links in the description below so I will be really really happy to see you in our community and also don't forget to leave a comment on what you want to learn next thank you and let's dive to the code now thank you [Applause] foreign [Music] let me first explain or give you some situations where we need to use criteria queries before even going and diving into the code so first of all what is criteria queries the criteria queries it enables us to write queries without doing row SQL as well as give us some object-oriented control over the queries and which which is one of the main features of hibernate so the criteria API allows us to build up a criteria query object programmatically and where we can apply different kind of filtrations rules and logical conditions so this is the definition of the global definition of the criteria queries now let me give you the example or the use case where we need to use or where we we don't have the choice but using criteria queries and in this case no no SQL no jpql no GPA repositories will enter our need except criteria queries so imagine we have or we want to perform a search action and in the search action we have multiple feeds and multiple fields and okay so these fields we are not or we should not provide all of them so let's say let's take the previous example that we did in the past videos so let's say I want to search employees by first name last name email birth at the date of birth also by role and so on so forth but when I want to perform the search as a user I can provide just the first name so I I'm not I'm not obliged or I should like it's not a must that that I need to provide all the all the inputs or all the attributes so in this case what I have to do I have to do a lot of if else's in the back end in order just to write or to perform or to prepare my SQL queries means I'm gonna write some query like select start from employee where and then the first test if first name is not null so then I add first name and the next one if last name and and and and so on so forth and this is not easy at all so the solution for this is hibernate criteria or criteria queries object so this is the use case or this is when mostly we use this so now I guess now you understand what we will Implement together let's go ahead and dive and just write some code to use or to implement the criteria queries now we will use the classic namings or the classic approach so we will not use repositories or that one of the repositories like GPA repositories or crude and so on so forth but we need to implement our own [Music] Dao or Dao layer which is the data access object so this means like this means we need to implement our own repository to do this stuff so this is our project and in here I will create a class I'm gonna call it for example employee repository so I want to perform um search sorry not repository but I'm just you can call it repository or Dao or as you like but I'm just gonna call it employee search doubt just to be consistent with what we will Implement inside this Dao class so first of all we need to do is we need to annotate this one with repository annotation so we tell spring that this is a repository annotation all right now what we will do we will just create one first method the first one is public and they want to return a list of employee and for example find all by simple gray the first one I'm gonna just call it like that find all by simple query and I will tell you why I'm calling it this way so I will require a string first name a string last name and for example let me just as a reminder see what do we have here so we have the ID first name last mail email birthday Troll and so on so forth so let's just I don't know a date birth date or let's stick just this string email so these are the the fields that we want to use to search for the employees okay so now I will explain what we exactly need in order to to perform or to create a criteria queries so first of all we need to create an object of type criteria Builder and it's from the Java X persistence criteria and I'm just call call it criteria Builder and this one I need something or some object to access or to get the criteria Builder and in order to do this we need to inject here we need to inject an entity manager so in this I'm just gonna call it em for for shortcut and now we can do em dot get criteria Builder so this one will return the criteria Builder but one thing we need to pay attention here we need to inject this entity manager whether we create a Constructor and we have the and we pass this entitymanager as a parameter or just have an auto wired or other autowired keyword here or we can use the required args Constructor from lombok in order to inject in order to inject the Constructor for us so you need to make the field that you want it to be included in the Constructor here you it should be final okay so now we created our criteria Builder now I will next create a criteria criteria query and this criteria query can give it a type it's a generic type or it's a generic one or generic class and here it's gonna be employee I want to do criteria query for employee and this one I'm gonna call it criteria query equals criteria Builder dot create query and here we need to pass the result class and in this case it's going to be employee dot class all right Next Step here I'm just gonna add a comment here we will do select star or select select from let's say select star it's select star from employee so how can we write this using objects it's so simple we need to use not toot Roots so here we need to use an object or a class called root from the Java x dot persistence.criteria and this root also it takes uh it takes a class or a type as a parameter so I will create a root of type employee because I want to do or I want to to create a query means this this means select from some class and here we want to select from employee dot class of course so here this this is the equivalent of Select star from employee now I'm gonna prepare my wear clothes for example I'm gonna prepare the wear clothes let me put this in uppercase so preparing the workloads means that we need to prepare some predicates and each predicate is each predicate is a close so for example I want to have a select star from employee where first name like something like I want to to find all the employees that the first name is some like includes Ali for example or the keyword Ali or alibu so we will do it like so first of all we need to do a predicate predicate and it's always from the same package so this predicate it's gonna be for example let's say first name come on first name predicate okay and this one equals criteria Builder and here what I want to do is for example as I mentioned I want to do like means I want to search okay let me do this let me put it into a new line so here what I want to do is this one is preparing the the workloads and here I want to do where something like that so I want to do where first name like something like this for example like percent percent Ali so anything or any employee that where the first name contains the word Ali so here well if I want to provide or I like I have to use first of all the keyword like so we have a method called like and for this method we need to use root dot get and here we need to provide the the as you can see here the get so we need to provide the attribute name and the attribute name here its first name and the first name here you need to refer to the column name in the database all right so and then we need to provide the pattern or the object and here what we want to use is something like this and here we want to use our first name and we are missing this percentage so percentage in in SQL means that means includes so now we have our first predicate first name predicate I'm just gonna copy paste this one I'm just gonna copy paste this one and I will call it last name predicate last name and here also it's gonna be last last name same for this one also I will have the same for email predicate and here it will be email and email here okay so now I have all the predicates ready to use now the way I'm gonna use this so here we have this where first name like where last name like where email like so now what I'm what are we missing here so we are missing the Boolean uh the we are missing the operation or like The Logical operation not the Boolean but the logical operation so do I want to have first name equals and last name or its first name or or its first name or email or or last name and so on so forth so for that for that I'm gonna create a new predicate or predicate for example predicate and the same it's equals criteria Builder Dot and here I can provide or I can also provide and and as you can see here for the or and the end for example let's say because you are doing a search so if I provide or if I use the or method I can provide a table or a list of predicates so here I'm gonna provide first name predicate last name predicate and email predicate so let me just align it so you can see it so let me format the code now this is my all predicate so here the query or the output query or the let's say final query here the final query is going to be select star from employee where first name like this and here I'm gonna just make the query or last name like this one and so on so forth Ali for example or coding and so on and so forth so this is the query that will be built out of this predicate now the last step we need to do is criteria query dot where and for the where we need to pass also a list of predicates so we can weather pass them all or we can pass only this one and in this case we need to pass this one because we want this is the the query that we want to that we want to write and here we can have this or predicate okay uh all right also we can have for example we can have for example something else we can say we can say here let me here let me say for example first name and last name uh here first name or last last name predicate and here I want my query to be or last name like and email like this so just just an example I want I want to show you how we can play with predicates so now here in the workloads what we will do we will do a criteria Builder here again we will use our criteria Builder and we will do or we will use the and predicate and and predicate it's going to be the first name and email entity manager so here we we created or we changed our query into this so instead of having or for all of them I need to remove this one from here so here we have our create select star from employee where first name like this or last name like this one and here we use the and email like this let me just extract this one I'm gonna call it um and email predicate we can use whether the the bar keyword or we can use just we can create a type for this so here now we can use and email predicate for example but I think it might be more readable the first way all right let's let's move on let's move on now we finished writing our query everything is done and we did all the predicates that we need now let's use or let's get the result now we will need an object or we will need an object from the same package and this object is called typed query and here we also can pass a class because it's a gene it's a generic and here it's it's going to be our final query and this one equals entitymanager dot create query and we need to pass the criteria query that we were building since the beginning so now all the thing and the last thing that we need to do is return query dot get results list and this way we just created a query created a criteria query that will gather all this information so now we our method our method requires these three parameters I have a question for you imagine that we have a search criteria or search request we have an object that contains first name last name email first name last name and email and any of them can be any of them can be null so we will see next in a few moments how we can handle or how we can deal with this situation first of all let's go okay so I need first to move this employee search dial to I'm gonna create a DOT package and put it in here or I can call it search or like you can call it whatever you want inside this package I will create a search request object like this is the object that we will get from the front end or like from our controller to our down method so this guy will contain or will have just an example I will I will reproduce the same thing but in a different way okay let me add let me comments here and here and also all this should be private do not forget the getter and setter uh Setter annotation and our object is ready to use let's close this and close this guy too so this is the first method first method I'm just gonna collapse it and now let's create a second method it's going to be public also a list of employee and this one we will call it find all by let's I'm just gonna call it criteria not Advanced criteria just just criteria and this one it will take an object object of type search request I will just call it request so you see first of all we have one difference here we are requesting the parameter so all of them are mandatory of course we can pass null as values but this in this request we don't know but here like the difference that here we know that we have these values but within this request we don't know if we have values or not so first of all first of all let's go ahead and create our criteria Builder let's call it again criteria Builder equals entitymanager dot get criteria Builder next our criteria query of type employee I will call it criteria query equals criteria Builder Dot create query of employee dot class so now we have almost everything next what I will do I will create a list of predicates so here I will just call it predicates equals new arraylist so in order not to fall into the null pointer exception so here as usual it's going to be the select from employee and we need for that to create a root object and this route is of type employee two I would just go call it root equals criteria query Dot from employee class okay now our root object is ready here we will perform just few tests first of all if request dot get email or let's let's start with the first name is null is not null so if we have this what we will do we will create a predicate as we did before first name predicate equals criteria Builder dot like and we will use the same code as we did before and here like let's uh let me just align it I will not align it but put it in a new line so it always still visible and here it's root dot get and here we need the attribute name and the attribute is first name and then we need the value and the value is as we did before it's gonna be something like that percentage here and percentage here and this it's gonna be request dot get first name all right and next what we will do it's going to be predicates dot add first name predicate I will just go ahead just to make this a bit faster I will just go ahead and copy paste it two times and this one is going to be last name here also last name and last name I will rename this one to last last name also uh this one is gonna be email same here same here and let's rename this one I will call it email predicate all right so now we have uh we have everything so what we need to do now is as simple as this view or this line of code so here it's going to be our criteria query dot where and here for example I want it's a search so when we perform a search access action so it should be always uh Etc there is a typo here so it should be always or a predication like or this or this or this so that's that's why we call it search unless we have something specific so we can implement it later on okay so here it's gonna be where and now and inside this one is going to be the criteria Builder dot or and in this or it's going to be predicates dot to array and for the array we need to pass a new predicate of 0 and it will return a table of predicates this this method will return a tape a table of predicates so once all this is done we need our typed query and this app query of type employee and I will call it just query or finder query and it's going to be m dot or our entitymanager DOT create query of criteria uh or is my object and my criteria query and finally as we did before it's gonna be return query dot get result list so this is how or in this way we just implemented we just implemented something that which is a bit more advanced but using all always criteria queries and as I mentioned this is a bit Advanced because we need to handle a little bit the way we want we want to do this also you can add some more checks and for example using the using the string utils class and check if it's not blank or if it's not null and so on so forth so I hope this was helpful for you see you in the announcement
Info
Channel: Bouali Ali
Views: 2,237
Rating: undefined out of 5
Keywords:
Id: qpSasUow1XI
Channel Id: undefined
Length: 30min 7sec (1807 seconds)
Published: Wed Nov 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.