Exercise 08: Entity Listeners - jkneal/spring-angular-train GitHub Wiki

Goals

In an earlier exercise we added a dateCreated field to our mapped superclass. There is also a new lastUpdateDate property which we'll use to track the last time a modification to an object is saved to the database. Now that we have entity listeners let's see how we can use them to keep track of this information.

Instructions

  1. Review the new tests in the StoreRepositoryTest class

  2. Run the tests and verify the two new tests are failing

  3. Review the new property in the GenericEntity class

  4. Add a method with @PrePersist which updates the dateCreated and dateLastModified properties to the current time before the object is saved to the database

  5. Run the tests and verify the testCreationDateSet test now passes

  6. Add a method with @PreUpdate which updates the dateLastModified to the current time before the object is saved to the database

  7. Run the tests and verify the testLastUpdateDate test now passes

Verification

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

Bonus Exercise

If you have additional time you can implement other methods using the various listener annotations which log when they are called. You can then execute the tests to see when each method logs when it is called in the lifecycle.