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.
- Review the method signatures in
StoreRepositoryand you will notice there are placeholder methods for an implementation of this interface inJpaStoreRepository.
- Note: There are two extra methods (findAll and findByNameAndOwner) which are in the interface but we will not implement them during this exercise.
-
Open
StoreRepositoryTestand 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. -
Implement the find, save and remove methods in
JpaStoreRepositoryand 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.
-
Remember to run the tests after the changes you make until they all pass
-
Review the
testRefreshandtestRefreshAfterFlushto see how the refresh and flush methods on the EntityManager could be used
Verification
- If you haven't ran the test case do so now and once all of the tests are passing then you are done.