Conteúdo do Curso
Java Data Manipulation with Hibernate
Java Data Manipulation with Hibernate
`Task` Entity Implementation
It's time to create the final entity in our small project. Employees should have tasks. Therefore, the last entity in the project will be the Task
entity.
Entity Creation
Our task entity should have fields such as:
- ID;
- Title;
- Description;
- Deadline;
Employee
assigned to the task;- Task status.
Pay attention to the last point. The task status can easily be placed in an enum
since we will have only 5 statuses:
- Not started;
- Started;
- In process;
- Almost finished;
- Done.
In the code, this enum will look like this:
With this enum
, the Task
entity will look like this:
Now we need to implement the DAO layer. Let's not dwell on this for too long, as you yourself have done this many times before. The Task
entity has the following methods in the DAO interface:
The implementation of this DAO interface is the same as for other entities, so let's move straight to the service layer, where two methods of interest are also defined.
- Update the employee who is assigned this task;
- Update the task status.
These are the methods that managers and employees will use in our system. As the application evolves, we will add new methods that employees and managers need. Because we have made our code extensible, adding a new method is not a problem for us, as it will not conflict with other methods of this application. Everything is isolated from each other, and most methods of the Employee
entity (for example) do not affect the methods of the Task
entity. With some exceptions, of course, but more than half of the methods are isolated.
Let's finally take a look at the methods in the service layer:
The implementation of the last two methods will look like this:
There's nothing complex here either. Everything follows the template: setting a new Employee
or Status
and then updating the Task
in the database.
Now, we need to test the functionality of this entity and the layers we have implemented. To do this, let's create 2 employees and one task in the main function. The employees will look like this:
The created object Task
will look like this:
Now let's take a look at the functionality of the code:
Great, the foundation of our application is ready. We have implemented the logic for interacting with the database. In the next chapter, we will summarize and save this project as we need it later.
Obrigado pelo seu feedback!