Card 05/ 06
Practice: Naming a Derived Query Method
Two conditions joined with And, and a comparison keyword, spelled as a method name.
A derived query method's name is built from findBy, the field names of the entity (the class mapped to a table), and keywords such as And, Or, GreaterThan and OrderBy. Write the method name, with its parameters, for this query: find all rides for a given riderId whose fare is greater than a given amount.
Then say what would happen if you misspelled riderId in the name, and when exactly you would find out, before you open the answer.
See a solution
List<RideEntity> findByRiderIdAndFareGreaterThan(Long riderId, double fare);RiderId and Fare name the two fields, And joins the conditions, and GreaterThan turns the second into a comparison. The parameters follow the order the fields appear in the name.
A misspelled field name stops the application from starting, because Spring Data parses every derived query name at startup and cannot find the field.
Adding OrderByFareDesc to the end, as in findByRiderIdAndFareGreaterThanOrderByFareDesc, sorts the result with the most expensive ride first.
← → move through sections and cards