DEV Community

Kudzai Tsapo
Kudzai Tsapo

Posted on

Making an HR software with Spring Boot Part V

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;

...

Enter fullscreen mode Exit fullscreen mode

Open Job.java and put the following code:


...

@OneToMany
private List<Employee> employees;

...

Enter fullscreen mode Exit fullscreen mode

When you're done with that, put the following code inside LeaveDay.java:


...

@ManyToOne
@JoinColumn(name = "employees_ecnumber")
private Employee employee;
...

Enter fullscreen mode Exit fullscreen mode

Afterwards, put the following code inside Misconduct.java:


...

@ManyToMany(mappedBy = "misconducts")
private List<Employee> employees;

...

Enter fullscreen mode Exit fullscreen mode

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)