Зміст курсу
Java Data Manipulation with Hibernate
Java Data Manipulation with Hibernate
`update` operation for Employee and Department
Employee Update
Let's start adding the final touches to the fundament of our project, and now we need to implement what you already implemented in the previous chapter for the Role
entity. Our task now is to implement the same functionality for the Employee
entity. We are interested in 2 methods:
- Full
employee
update; - Updating an employee's
salary
; - Role and Department updates are already implemented.
Let's start with the first point. If you successfully completed the previous task, understanding how this works shouldn't be a problem for you.
We reassign all the entities' fields with the updated fields.
Here's how it will look in the code:
As you can see, there's nothing complicated about this. We simply assign all the old employee fields to the new fields. This way, we update the employee's information in the database.
Now let's move on to the next point, where we need to update this employee's salary.
We have several options for changing the salary:
- Increase the salary by a certain amount;
- Decrease the salary by a certain amount;
- Increase the salary by a certain percentage;
- Decrease the salary by a certain percentage.
As you can see, there are many options, but they all have the same purpose. Therefore, a good solution would be to overload this method. This will be a way to adhere to polymorphism in OOP.
Note
There's also an option to write just one method, into which we'll pass a specific
enum
to determine which method to use. However, this practice is uncommon in Java development, as it doesn't fully adhere to OOP principles. You can try implementing this yourself, but we'll use polymorphism.
So, let's write methods for the first and second points:
In this code, there's nothing complicated either. We retrieve and modify the employee's salary
, then update the employee in the database using the session.merge()
method.
Here, I used simple addition and an increase by a percentage.
Let's implement similar methods, but this time for decreasing the salary. Perhaps the employee will show poor performance, and we'll have to penalize them.
Implementing the methods will be almost identical; we just need to change the plus sign to a minus sign.
Here's how it will look in the code:
The only difference we made besides changing the sign is adding a check to ensure the amount does not become negative. Otherwise, it would be too harsh on the employee.
But you might have noticed that these methods perform some operations. Therefore, we need to move them to the service layer. In the DAO layer, only one update method should remain. It's not difficult to do.
Here's how the implementation will look in the service layer:
Now, let's test the functionality of these methods in the Main
class to ensure everything is working:
Great, we successfully put Bob
through some challenges. In the next chapter, your task will be to implement similar methods for the Department
entity.
Дякуємо за ваш відгук!