DEV Community

Kudzai Tsapo
Kudzai Tsapo

Posted on

Making an HR software with Spring Boot Part IV

Welcome back ...eh ... if you're still following this tutorial. Anyway, I'm going to start by explaining what happened in the last tutorial and why ...ish.

So, I'll start with explaining the application.properties thing which I told you to do. First the line spring.jpa.hibernate.ddl-auto=update is a spring configuration which is used by Hibernate for DDL generation. I'm not going into more details, since the tutorials would span for 100 parts ... which ... isn't really a good idea. Anyway, spring.datasource.url is a configuration for specifying the database host, port, name and type. As for the rest of the configs, they're just the database credentials.

Welp, now we're done with the configs, let's move on to the code. Instead of writing getters and setters for the class, I used Lombok, which does that for me. That's why there are the @Getter and @Setter annotations. Again, I'm going to skip the entire explanation for how they work.

@Entity annotation is used to tell JPA that we're going to be persisting the Employee objects into the database. In most cases, the name of the table in the database and the name of the entity will not be the same. This is why I used the @Table annotation, so that I can specify the name of the table. Of course, there are more things I could have specified, but for now, this explanation suffices.

And then there are the column attributes. Honestly, I'm gonna skip explaining those because I feel like I'm running out of space.

Now, let's go back to work. Where was I? Right, we have the employee model defined. Let's define the rest of them. For the record, I'm going to be posting the code here ... if you have any questions, I'm sure you can post them in the comments. Now, the Project Manager is angry because he believes you're wasting time. So, let's get back to work!!

First, we'll start with jobs ... So, create a file under models, with the name Job.java and paste the following code ...

package com.dull.piqued.models;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@Entity
@Table(name = "jobs")
public class Job {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(nullable = false)
    private String title;

    @Column
    private double minSalary;

    @Column
    private double maxSalary;

    @CreationTimestamp
    private Date createdAt;

    @UpdateTimestamp
    private Date updatedAt;
}
Enter fullscreen mode Exit fullscreen mode

After the Job model, we have the Department model.

package com.dull.piqued.models;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@Entity
@Table(name = "departments")
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long departmentId;

    @Column(nullable = false)
    private String name;

    @CreationTimestamp
    private Date createdAt;

    @UpdateTimestamp
    private Date updatedAt;
}
Enter fullscreen mode Exit fullscreen mode

And then leave days:

package com.dull.piqued.models;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@Entity
@Table(name = "leave_days")
public class LeaveDay {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(nullable = false)
    private Date startDate;

    @Column(nullable = false)
    private Date endDate;

    @Column
    private Date dateApproved;

    @Column
    private String leaveType;

    @Column
    private String description;

    @Column
    private String leaveStatus;

    @CreationTimestamp
    private Date createdAt;

    @UpdateTimestamp
    private Date updatedAt;
}
Enter fullscreen mode Exit fullscreen mode

Following that, we have misconducts. Remember, HR is supposed to record all misconducts that an employee does, and then if they are suspended for eternity, that is supposed to be recorded as well.

package com.dull.piqued.models;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@Entity
@Table(name = "misconducts")
public class Misconduct {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(nullable = false)
    private String name;

    @Column
    private String description;

    @Column
    private Date dateOccurred;

    @Column
    private String punishment;

    @Column
    private String punishmentDate;

    @CreationTimestamp
    private Date createdAt;

    @UpdateTimestamp
    private Date updatedAt;
}
Enter fullscreen mode Exit fullscreen mode

I know the punishment could probably have gone into a different model, but since I'm too lazy to do that, we'll leave it there ... Now that we are done with the models, we are now going to be implementing relationships 😬 in the next tutorial ... chao 👋

Top comments (0)