Task 3: Setting Up a One-to-Many Relationship
In this application, any one Job
object is affiliated with one employer while one Employer
may contain several jobs.
Now that you have set up persistence for the Employer
and Skill
classes, it is time to update the Job
class
to make use of these. Job
is already using the Spring Data framework to be persistent and now you’ll update its
Employer
field to create a one-to-many relationship. You’ll also add a field on Employer
to list the jobs associated
with each instance.
Add a jobs
Field to Employer
- Within
Employer
, add a private propertyjobs
of typeList<Job>
and initialize it to an emptyArrayList
. After we set up theJob
class to work withEmployer
objects, this list will represent the list of all items in a given job. We’ll do this in a bit. - Use the
@OneToMany
and@JoinColumn
annotations on the jobs list inEmployer
to declare the relationship between data tables. Recall that this annotation needs aname
parameter. What should its value be?
Update Job
Model
- Since the
Job
model class hasid
andname
fields, it too can inherit fromAbstractEntity
. Update the class definition ofJob
to extendAbstractEntity
. Remove the redundant fields fromJob
. - Replace the type of the field
employer
to be of typeEmployer
. You will also need to refactor the affected constructor and getter and setter that use this field. - Add the
@ManyToOne
annotation on the fieldemployer
Updating HomeController
As above, there is a test in TestTaskThree
that needs to be uncommented. There is only one, and it is near the end of the file. Do that now.
Open TestTaskThree
in IntelliJ and find this test.
- Select the entire commented-out method.
- Uncomment the method by using
cmd+/
on Mac orctrl+/
on Windows.
If you do not uncomment this test, your code will not pass the autograder.
We’ll make several updates here. Similar to what you have done in Part 1, several of the methods in HomeController
are
missing code because the class has not yet been wired with the data layer yet.
- Add a field
employerRepository
annotated with@Autowired
. - A user will select an employer when they create a job. Add the employer data from
employerRepository
into the form template. The add job form already includes an employer selection option. Be sure your variable name for the employer data matches that already used intemplates/add
. - Checkout
templates/add.html
. Make a mental note of the name of the variable being used to pass the selected employer id on form submission. - In
processAddJobForm
, add code inside of this method to select the employer object that has been chosen to be affiliated with the new job. You will need to select the employer using the request parameter you’ve added to the method.
An employer only needs to be found and set on the new job object if the form data is validated.
Test It with SQL
You made a lot of changes! Great work.
Assuming you don’t have any compiler errors , start up your application. Don’t forget to start your SQL server. Make sure you can create a new job object from the Add Jobs form, selecting a pre-existing employer.
Then, make sure the data has been saved in your job table. You should see a column for employer_id
, corresponding to the employer object selected for the new job.
You have changed the architecture of your job table. You will still be able to add a new entry that has anemployer_id
column but you’ll note that job still has the now defunct employer
column. You can keep your database clean by removing the job table. It will be recreated when you run the application again.
- SQL TASK: In
queries.sql
under “Part 3”, write the SQL statement to remove the job table.
The List and Search functionality still isn’t quite fixed so to view a job in the application, make a note
of the job’s id in the SQL table. Back in your browser, enter the path for /view/{jobId}
.
When everything works, move on to Part 4 below.