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
- 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) |
-
Add some example data for a customer in the data.sql file
-
Create a new class in the
edu.train.namepackage namedNameand mark it as an embeddable class -
Add
firstNameandlastNameproperties to theNameclass which map up to the FIRST_NAME and LAST_NAME columns added earlier in the exercise -
Create a new class in the
edu.train.addresspackage namedAddressand mark it as an embeddable class -
Add
line1,city,stateandpostalCodeproperties to theAddressclass which map up to the ADDR_LINE1, ADDR_CITY, ADDR_ST, and ADDR_POST_CD columns added earlier in the exercise -
Map up the id, username,
NameandAddressproperties in theCustomerclass
Verification
-
Start the training application and go to the app home page
-
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
-
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.
-
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.