Now, let's talk about a topic that's not so near and dear to my heart: relationships 😬. Of course, I'm talking about the only relationships that programmers are really good at ... database relationships!
Anyway, before we go into that, there's something I forgot to put at the end of the application.properties
. Copy paste this line:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
I'll not be explaining what this line does (I'm lazy lol). Now that we're done, let's continue on to relationships... (cough cough). Right, we know that an Employee has a Job, and is under a Department, is allowed to take many LeaveDays, and can misbehave multiple times. From this alone, we now have our database relationships...
Open the Employee.java and paste the following code (which of course I won't be explaining...):
...
@OneToOne
@JoinColumn(name = "job_id")
private Job job;
@OneToOne
@JoinColumn(name = "department_id")
private Department department;
@ManyToMany
@JoinTable(name = "employee_misconducts",
joinColumns = @JoinColumn(name = "employee_id"),
inverseJoinColumns = @JoinColumn(name = "misconduct_id"))
private List<Misconduct> misconducts;
@OneToMany(mappedBy = "employee")
private List<LeaveDay> leaveDays;
...
Open Job.java and put the following code:
...
@OneToMany
private List<Employee> employees;
...
When you're done with that, put the following code inside LeaveDay.java:
...
@ManyToOne
@JoinColumn(name = "employees_ecnumber")
private Employee employee;
...
Afterwards, put the following code inside Misconduct.java:
...
@ManyToMany(mappedBy = "misconducts")
private List<Employee> employees;
...
And with that, we can say we're done-ish. Obviously, there are a lot of things that can be improved, but then again, we're not perfectionists ( I know I'm not! ). Anyway, in the next tutorial, we are going to finish up on the API by making repositories, controllers and DTOs. Enjoy your day ... or night ... chao 👋
Top comments (0)