---
title: "Discussion on \"Replace a JPA entity with a DTO\" | Hashnode"
description: "Discussion on \"Replace a JPA entity with a DTO\". This article will show you how to create a DTO, and replace a JPA Entity with a DTO to use in the Response Body."
url: "https://hashnode.com/posts/replace-persistence-entity-with-a-dto-object/6294a0244dbee2dc251e3843"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Replace a JPA entity with a DTO](https://techwithmaddy.com/replace-persistence-entity-with-a-dto-object?utm_source=hashnode&utm_medium=feed)

This article will show you how to create a DTO, and replace a JPA Entity with a DTO to use in the Response Body.

May 30, 2022 · 4 min read · techwithmaddy.com  
Updated Aug 17, 2023  
Read the article

-   [#java](https://hashnode.com/tag/java)
-   [#spring](https://hashnode.com/tag/spring)
-   [#springboot](https://hashnode.com/tag/springboot)
-   [#design-patterns](https://hashnode.com/tag/design-patterns)
-   [#best-practices](https://hashnode.com/tag/best-practices)

## Written by

[Maddy](https://hashnode.com/@Maddy25)

@Maddy25 · London, UK

  
[View profile](https://hashnode.com/@Maddy25)

Technical Writer @ Cloudflare

Software engineer turned technical writer.

## Responses 1

Markdown supported · ⌘↵ to post

[Rainer Scholz](https://hashnode.com/@rainer619)

[Sep 9, 2022](https://hashnode.com/posts/replace-persistence-entity-with-a-dto-object/6294a0244dbee2dc251e3843/comment/631b4a3df73f4734bc0517e8)

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 `CustomerDTO` requires some kind of resource identifier. I used the primary key of the database table.
