Exercise 04: Embeddables - jkneal/spring-angular-train GitHub Wiki

Goals

We now need a customer class to keep track of people who purchase our products. The customer should minimally store a username, their name and an address.

Instructions

  1. Add a new table to hold customers in the schema.xml file with the following constraints:
Field Type Constraints
ID IDENTITY Primary Key
USERNAME VARCHAR(20)
FIRST_NAME VARCHAR(40)
LAST_NAME VARCHAR(40)
ADDR_LINE1 VARCHAR(120)
ADDR_CITY VARCHAR(120)
ADDR_ST VARCHAR(30)
ADDR_POST_CD VARCHAR(12)
  1. Add some example data for a customer in the data.sql file

  2. Create a new class in the edu.train.name package named Name and mark it as an embeddable class

  3. Add firstName and lastName properties to the Name class which map up to the FIRST_NAME and LAST_NAME columns added earlier in the exercise

  4. Create a new class in the edu.train.address package named Address and mark it as an embeddable class

  5. Add line1, city, state and postalCode properties to the Address class which map up to the ADDR_LINE1, ADDR_CITY, ADDR_ST, and ADDR_POST_CD columns added earlier in the exercise

  6. Map up the id, username, Name and Address properties in the Customer class

Verification

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

  2. Click on one of the customer links and verify the resulting JSON contains all of the values you added in data.sql. Pay close attention to the JSON structure and notice that the name and address objects are nested JSON hashes instead of being included directly in the parent object like we've seen before.

Bonus Exercise

  1. The STORE_T table in our database also has the same address fields as our CUSTOMER_T table. Let's reuse the same embeddable there as well.

  2. Restart the application and visit the store link. When everything is working correctly you should also see an embedded address object in the JSON which is returned.