Labs
NOTE: The following exercises ask you to build entities for some of your SQL tables. Not every column will have a corresponding field in the Entity after this lab. That is totally fine and will not cause any problems.
Entities will be created in the JPAVideoStore/com.example.jpavideostore.entities package.
Client classes will be created in the JPAVideoStore/com.example.jpavideostore.client package.
-
Create an
Addressentity. -
Add the following fields and map them to the corresponding database columns:
-
id -
street -
street2 -
state -
city -
postalCode -
phone
-
The address table looks like this:
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| address | varchar(50) | NO | | NULL | |
| address2 | varchar(50) | YES | | NULL | |
| city | varchar(50) | NO | | NULL | |
| state_province | varchar(20) | NO | | NULL | |
| postal_code | varchar(10) | YES | | NULL | |
| country_code | char(2) | YES | MUL | NULL | |
| phone | varchar(20) | NO | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
-
Add getters/setters and a
toStringimplementation. -
Remember to add
Addressto your persistence.xml -
Create a new class
AddressClientin yourcom.example.jpavideostore.clientpackage -
Add a
mainmethod that does afindto retrieve theAddressentity with primary key5. -
Print it out using its
toStringimplementation. -
Create a
Staffentity. -
Map the fields to their corresponding columns:
-
id -
firstName -
lastName -
email -
active -
username -
password
-
The active field will be a boolean.
The staff table looks like this:
+---------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| first_name | varchar(45) | NO | | NULL | |
| last_name | varchar(45) | NO | | NULL | |
| address_id | int(10) unsigned | NO | MUL | NULL | |
| email | varchar(50) | YES | | NULL | |
| store_id | smallint(5) unsigned | NO | MUL | NULL | |
| supervisor_id | int(10) unsigned | YES | MUL | NULL | |
| active | tinyint(1) | NO | | 1 | |
| username | varchar(16) | YES | UNI | NULL | |
| password | varchar(41) | YES | | NULL | |
+---------------+----------------------+------+-----+---------+----------------+
toString implementation.
-
Remember to add
Staffto your persistence.xml -
Create a
StaffClientclass andfindto theStaffentity with primary key1. -
Create a
Languageentity with mapped fields for: -
id -
name
The language table looks like this:
```
+-------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
+-------+----------------------+------+-----+---------+----------------+
```
-
Add getters/setters and a
toStringimplementation. -
Remember to add
Languageto your persistence.xml -
Create a
LanguageClientclass andfindto theLanguageentity with primary key1. -
Create a
Filmentity. -
Map the following fields to their corresponding columns:
-
id -
title -
description -
releaseYear -
rentalDuration -
rentalRate -
length -
replacementCost
-
The film table looks like this:
+------------------+---------------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------------------------------------------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | MUL | NULL | |
| description | text | YES | | NULL | |
| release_year | year(4) | YES | | NULL | |
| language_id | smallint(5) unsigned | NO | MUL | NULL | |
| rental_duration | tinyint(3) unsigned | NO | | 3 | |
| rental_rate | decimal(4,2) | NO | | 4.99 | |
| length | smallint(5) unsigned | YES | | NULL | |
| replacement_cost | decimal(5,2) | NO | | 19.99 | |
| rating | enum('G','PG','PG13','R','NC17') | YES | | G | |
| special_features | set('Trailers','Commentaries','Deleted Scenes','Behind the Scenes') | YES | | NULL | |
+------------------+---------------------------------------------------------------------+------+-----+---------+----------------+
releaseYear and length, use int as the data type. For the rentalRate and replacementCost, use double.
-
Add getters/setters and a
toStringimplementation. -
Remember to add
Filmto your persistence.xml -
Create a
FilmClientclass andfindto theFilmentity with primary key1.