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

  1. Review the new tests in the StoreRepositoryTest class. Execute them to make sure the new tests still fail.

  2. Implement the findByNameWithWildcard method in the JpaStoreRepository. 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.

  3. Run the tests again to verify the testFindByNameWithWildcard and testFindByNameWithWildcardMatchAll tests now pass

  4. Implement the findByNameAndOwner method in the JpaStoreRepository. 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.

  5. Run the tests again to verify the testFindByNameAndOwner test now passes

  6. Implement the countStoresByStatus method in the JpaStoreRepository. 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
  1. Run the tests again to verify the testCountStoresByStatus test now passes

  2. Implement the findStoresCreatedToday method in the JpaStoreRepository. 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)
  1. Run the tests again to verify the testFindStoresCreatedToday test now passes

Verification

  1. The test cases will pass when the exercise is complete