So i have 3 entities:
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
private BigDecimal salary;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id")
private Address address;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "company_id")
private Company company;
}
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String city;
private String street;
private String homeNumber;
}
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "address_id")
private Address Address;
@OneToMany(mappedBy = "company")
private List<Employee> employees = new ArrayList<>();
}
When I try to update using the methods below, the foreign keys in the employee(company_id, address_id) table are set to null.
public EmployeeDTO applyPatchToEmployee(EmployeeDTO employeeDTO, JsonMergePatch patch) throws JsonProcessingException, JsonPatchException {
JsonNode employeeNode = mapper.valueToTree(employeeDTO);
JsonNode appliedPatchEmployee = patch.apply(employeeNode);
return mapper.treeToValue(appliedPatchEmployee, EmployeeDTO.class);
}
public void updateEmployee(EmployeeDTO employeeDTO) {
Employee employeeToSave = new Employee(employeeDTO.getId(),employeeDTO.getFirstName(), employeeDTO.getLastName(), employeeDTO.getSalary());
employeeRepository.save(employeeToSave);
}
@PatchMapping("/update/{id}")
public ResponseEntity<?> updateEmployee(@PathVariable Long id, @RequestBody JsonMergePatch patch) throws EmployeeNotFoundException, JsonPatchException, JsonProcessingException {
EmployeeDTO employeeById = employeeService.getEmployeeById(id);
EmployeeDTO employeeByIdPatched = employeeService.applyPatchToEmployee(employeeById, patch);
employeeService.updateEmployee(employeeByIdPatched);
return ResponseEntity.noContent().build();
}
The problem is solved when I replace the updateEmployee method in the service with this:
public void updateEmployee(EmployeeDTO employeeDTO) throws EmployeeNotFoundException {
Employee foundEmployee = findEmployee(employeeDTO.getId());
Employee employeeToSave = new Employee(employeeDTO.getId(),employeeDTO.getFirstName(), employeeDTO.getLastName(), employeeDTO.getSalary());
employeeToSave.setCompany(foundEmployee.getCompany());
employeeToSave.setAddress(foundEmployee.getAddress());
employeeRepository.save(employeeToSave);
}
My question is if there is an option to do it differently. Finally, we may have a case where there will be, for example, 10 such fields and it will not be a very nice technique.
I also tried setting cascade type to persist but that doesn’t help either.
I realize that DTO does not contain the address and company, so without these setters the fields are set to null. However, I conclude that… (maybe I’m wrong because I haven’t been learning programming for very long) that we wouldn’t want to change the company and address in DTO, but change them somehow separately, e.g. in CompanyService and AddressService. Here we are interested in changing the employee’s data and maintaining the relationship with the company and address.
Thank you in advance!!
Damno is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.