0

I was wondering if it is possible to not use DTO and if there is any other approach, with JPA. Is it possible to create a new X object from the form input Y without first creating a DTO Z?

devo9191
  • 219
  • 3
  • 13
  • 3
    We do not *need* to use DTOs, however it is highly recommended ([Separation of Concerns](https://en.wikipedia.org/wiki/Separation_of_concerns)). Likewise, I would recommend to not use JPA entities in the business layer and rather pass along specialized DTOs to the persistence layer. Thus, we would have at least three classes per Entity: The outbound DTO, e.g. representing the HTTP request/response, the actual business entity and the JPA entity. – Turing85 Aug 20 '21 at 10:48
  • @Turing85 Thanks for your response, you make some interesting points and I agree with you wholeheartedly, but theoretically why would using the object directly be in violation of separation of concerns? – devo9191 Aug 20 '21 at 10:53
  • Because we couple the concern of an internal business representation with - for example - the persistence layer representation. A concrete case: it is often a good idea that database entities have a technical primary key (most likely a `BIGINT`), but the business layer may not need it. So why should we see the technical primary key in our business object? A similar argument can be made with the external representation. – Turing85 Aug 20 '21 at 10:56
  • As always it depends. – Simon Martinelli Aug 20 '21 at 11:30

0 Answers0