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
Skill classes, it is time to update the
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.
jobs Field to
Employer, add a private property
List<Job>and initialize it to an empty
ArrayList. After we set up the
Jobclass to work with
Employerobjects, this list will represent the list of all items in a given job. We’ll do this in a bit.
- Use the
@JoinColumnannotations on the jobs list in
Employerto declare the relationship between data tables. Recall that this annotation needs a
nameparameter. What should its value be?
- Since the
Jobmodel class has
namefields, it too can inherit from
AbstractEntity. Update the class definition of
AbstractEntity. Remove the redundant fields from
- Replace the type of the field
employerto be of type
Employer. You will also need to refactor the affected constructor and getter and setter that use this field.
- Add the
@ManyToOneannotation on the field
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.
TestTaskThree in IntelliJ and find this test.
- Select the entire commented-out method.
- Uncomment the method by using
cmd+/on Mac or
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
missing code because the class has not yet been wired with the data layer yet.
- Add a field
- A user will select an employer when they create a job. Add the employer data from
employerRepositoryinto 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 in
templates/add.html. Make a mental note of the name of the variable being used to pass the selected employer id on form submission.
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 an
employer_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.sqlunder “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
When everything works, move on to Part 4 below.