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
-
Review the new tests in the
StoreRepositoryTestclass -
Run the tests and verify the two new tests are failing
-
Review the new property in the
GenericEntityclass -
Add a method with @PrePersist which updates the
dateCreatedanddateLastModifiedproperties to the current time before the object is saved to the database -
Run the tests and verify the
testCreationDateSettest now passes -
Add a method with @PreUpdate which updates the
dateLastModifiedto the current time before the object is saved to the database -
Run the tests and verify the
testLastUpdateDatetest now passes
Verification
- 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.