Exercise 05: Mapped Superclass - jkneal/spring-angular-train GitHub Wiki

Goals

We have several objects in our system now, but we just realized that we need to track the time we created each object for auditing purposes. Right now the Order class is the only one which keeps track of this information. Rather than duplicating several column definitions let's use a MappedSuperclass to handle the mapping for all of our classes in a single place.

Instructions

  1. Create new superclass named GenericEntity

  2. Make product, order, store and customer all extend the shared superclass

  3. Add a new createDate property with a mapping that is similar to the one found in the Order class

  4. Remove the existing createDate property from the Order class since it is now redundant

Verification

  1. Start the training application and go to the app home page

  2. Browse through the data for all four classes and ensure the createDate data is being returned appropriately. (Note: the creation date for the product and customer classes is defaulted to the time when you start the application so it will change each time you start it)

Bonus Exercise

  1. Change the name of the CREATE_DT field in the PRODUCT_T table to CREATION_DT and update the entry in data.sql appropriately

  2. Add an attribute override annotation to the person class which uses a custom @Column annotation to change the name of the field

  3. Verify the field is still mapped up appropriately by restarting the application and verify the creation date is still returned appropriately

Cleanup

You may have noticed that Lombok started throwing a warning in your classes which extend another class. This happens because Lombok will not call equals and hashcode methods on the superclass unless it is explicitly instructed to call them. You can remove the warning by adding "@EqualsAndHashCode(callSuper=true)" so Lombok will call the methods on the superclass.