Replace a JPA entity with a DTO
Have you ever come across a Sonar vulnerability issue such as:
Replace this persistence entity with a POJO or DTO object.
This happens when you pass a persistence entity into the @ResponseBody of a REST call instead of a DTO object.
This article wi...
techwithmaddy.com4 min read
Hi Maddy,
I wonder how an
public CustomerDTO updateCustomer(CustomerDTO customerDTO)would be implemented by you?My straight forward solution would be:
public CustomerDTO updateCustomer(CustomerDTO customerDTO) { Customer customer = customerRepository.getReferenceById(customerDTO.getId()); customer.setFirstName(customerDTO.getFirstName()); customer.setLastName(customerDTO.getLastName()); // ... repeated for all fields customerRepository.save(customer); }Note: The
CustomerDTOrequires some kind of resource identifier. I used the primary key of the database table.