Learn how to create custom queries with Spring Data query methods.
- [Instructor] We need to look up the tour package by the name. The code is not provided in the JSON import file. We accomplish this by creating query methods in the TourPackageRepository interface. So let's open TourPackageRepository. So I want to declare a method that will give me the Tour Package. One name is a parameter. So I create TourPackage. Return it TourPackage and findByName parameter.
Now let's go to the service to invoke it. So what's going to actually be passed in here is the tourPackageName. Use the method findByName. Now you're probably thinking, "Wait a minute, Mariellyn. "You didn't implement the method." Yeah, you're right but let's try it anyway.
I'm going to run and debug. We'll pass this break point. Okay, we got the tourPackage. We hit this break point and it did return a tourPackage for Backpack Cal. Is it magic? Is it a miracle? Well, no, it's actually not magical at all if via introspection and reflection Spring implements the method under the covers for us.
But we must follow their rules when declaring methods and correctly map the entity properties to the method signature. So I'm going to get rid of that break point and just run it to the end and we see it created 30 tours. Query methods are a great code saver. Spring Data JPA eliminates ugly boilerplate querying code. To create a simple query method, you only need to declare the return type. Begin the signature with findBy, followed by an attribute name in camelCase and query parameters whose type matches the attribute type.
Query method signatures can span multiple entities. findByTourPackage looks up tours by the tour package but you must provide a reference to an already persisted tour package. If you do not have the tour package reference, you can look up the tour by the tour package code. The tour entity has a tour package attribute and tour package has a code attribute. Chain the attribute name and follow camelCase rules like we do in findByTourPackageCode. Spring Data JPA facilitates fast failure.
Here Region is misspelled. When Spring tries to instantiate the class to implement to a repository at system start up, it throws a PropertyReferenceException. With SQL, you would not know that there was a syntax error until the query runs. You can make complex queries with additional filter options such as and, or, in, less than, contains and like. Refer to the Spring Data JPA specification for all of the filter combinations.
As you can see, the signatures on the methods become longer as the query method becomes more complex. You could leverage complex JQL queries inside a repository interface by using the @Query annotation. Here, lookupTour runs the same query as findByTourPackageCodeAndDifficultyAndRegionAndPriceLessThan but the signature is much cleaner. @Query method signatures do not need to follow the declaration rules.
The scope of this course does not include JQL but you should be aware of this as an alternative. Refer to the Spring Data JPA specification for complete description. This has been a very brief overview of Spring Data JPA. I encourage you to explore this feature further as it quickly becomes the most popular and convenient method to interact with relational databases.
- Setting up the project
- Building, deploying, and launch the microservice
- Declaring Spring Data JPA repository interfaces
- Invoking repositories
- Using Spring Data query methods
- Exposing RESTful APIs with Spring Data REST
- Using the /search resource to invoke query methods
- Paging and sorting
- Declaring a new REST controller
- Creating HTTP methods for creating, reading, updating and deleting persistent data.