Entity update – foreign keys set to null

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!!

New contributor

Damno is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật