Exercise 11: More advanced JPQL - jkneal/spring-angular-train GitHub Wiki
Goals
This is the final exercise for the JPA module. In this exercise we will implement a few more methods in our StoreRepository:
- Find all stores by name which contains a wildcard (%)
- Find a single store by its name and owner
- Find the number of stores with a specific opened status
- Find all stores which have been created today
Instructions
-
Review the new tests in the StoreRepositoryTest class. Execute them to make sure the new tests still fail.
-
Implement the
findByNameWithWildcardmethod in theJpaStoreRepository. Use JPQL and the LIKE operator which will take in a string containing % as a wildcard character and return all of the stores which match that pattern. -
Run the tests again to verify the
testFindByNameWithWildcardandtestFindByNameWithWildcardMatchAlltests now pass -
Implement the
findByNameAndOwnermethod in theJpaStoreRepository. Use JPQL to create a query with name and owner parameters which will allow you to find a single store by its name and owner. -
Run the tests again to verify the
testFindByNameAndOwnertest now passes -
Implement the
countStoresByStatusmethod in theJpaStoreRepository. Use JPQL to create a query with a status parameter and uses the count function which will allow you to find a single store by its name and owner.
- Note: Keep in mind the count function returns a Long
-
Run the tests again to verify the
testCountStoresByStatustest now passes -
Implement the
findStoresCreatedTodaymethod in theJpaStoreRepository. Use JPQL to create a query which uses the CURRENT_DATE function to find all of the stores which have been created today.
- Note: You can also do basic addition with the CURRENT_DATE function as well (e.g. "CURRENT_DATE + 1" would be tomorrow's date)
- Run the tests again to verify the
testFindStoresCreatedTodaytest now passes
Verification
- The test cases will pass when the exercise is complete