Exercise 07: Basic Entity Operations - jkneal/spring-angular-train GitHub Wiki

Goals

Now that we can see how to use the EntityManager to perform operations to save state to the database and read information from it, we can use the EntityManager for database operations.

Instructions

To simplify the examples for this exercise the Store class has been modified slightly so it uses a generated ID instead of a composite key. The methods we are implementing here will also be more difficult to test with the user interface since we haven't built any screens yet so we will use integration tests for the next few exercises to test the changes we make.

  1. Review the method signatures in StoreRepository and you will notice there are placeholder methods for an implementation of this interface in JpaStoreRepository.
  • Note: There are two extra methods (findAll and findByNameAndOwner) which are in the interface but we will not implement them during this exercise.
  1. Open StoreRepositoryTest and review its contents. This is an integration test which exercises the methods on our interface. You can execute the test by right-clicking it and running it as a JUnit test. All of the tests should fail at this point.

  2. Implement the find, save and remove methods in JpaStoreRepository and use the test cases to verify that everything is working as expected.

  • Try using both persist and merge on the EntityManager in your save method to see the different behaviors that each exhibits with the test cases.
  1. Remember to run the tests after the changes you make until they all pass

  2. Review the testRefresh and testRefreshAfterFlush to see how the refresh and flush methods on the EntityManager could be used

Verification

  1. If you haven't ran the test case do so now and once all of the tests are passing then you are done.