Search posts, tags, users, and pages
This article will show you how to create a DTO, and replace a JPA Entity with a DTO to use in the Response Body.
Maddy
@Maddy25 · London, UK
Technical Writer @ Cloudflare
Software engineer turned technical writer.
Markdown supported · ⌘↵ to post
Rainer Scholz
Hi Maddy,
I wonder how an public CustomerDTO updateCustomer(CustomerDTO customerDTO) would be implemented by you?
public CustomerDTO updateCustomer(CustomerDTO customerDTO)
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 CustomerDTO requires some kind of resource identifier. I used the primary key of the database table.
CustomerDTO
Rainer Scholz
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.