title: REST development backend DTO vs frontend DTO
body: <p>What is the best approach when developing (JSON-based) REST services in terms of Data objects.</p>
  <p>Should I split database model (back-end) from the front-end model?</p>
  <p>Is it a good practice to always keep the DB related JPA Entities up to a specific layer and then convert them into a set of DTOs for the front-end?</p>
  <p>For example, 3 layer architecture:</p>
  <p><code>Controller</code></p>
  <p><code>Service</code></p>
  <p><code>Repository</code></p>
  <p>Should I confine the DB entities (annotated with JPA annotations) to <code>Repository</code> and <code>Service</code> layers</p>
  <p>And then make the <code>Controller</code> only operate with another set of UI 'entities' (DTOs)?</p>
  <p>This would require some kind of mapping between the 2 either automatic or 'manual'.</p>
  <p>This allows for 'thin' front end entities.</p>
  <p>For example in Backend we have the JPA annotations only and we have the owner as an <code>Account</code> reference:</p>
  <pre><code>@Entity
  public class Job {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      private String name;
      @ManyToOne(fetch = FetchType.LAZY)
      private Account owner;
  </code></pre>
  <p>But in the front-end layer I would have Jackson specific annotations. I don't need the whole <code>Account</code> object, but only its id in the UI:</p>
  <pre><code>class Job {
   long id;
   String name;
   long ownerId;
  }
  </code></pre>
  <p><strong>Update</strong>
  After experimenting with &quot;manual&quot; mapping, the conclusion is that it quickly becomes a mess.</p>
  <p>In my case, I wanted the <code>Repository</code> layer to return Entity (JPA) and the <code>Service</code> layer to do the mapping and return the Dto. So for getting data from the DB, this seems pretty affordable, there is only 1 mapping involved (from Entity to Dto). But, when it comes to creating / saving entities, the problem is bigger with composite objects. For example:</p>
  <p>Let's say I <code>POST</code> a <code>UserDto</code> (which contains <code>UserProfileDto</code> as a composite object) from the API client to the <code>Controller</code>. Now the <code>Service</code> layer will accept <code>UserDto</code>, but it will have to find the <code>UserProfileEntity</code> corresponding to that <code>UserProfileDto</code>.</p>
  <p>Also, Repository's <code>.save()</code> method returns the newly persisted Entity. Now this has to be mapped to Dto and then back to Entity if I want to use it further as part of another object (otherwise I will get the <code>object references an unsaved transient instance - save the transient instance before flushing</code> error).</p>
  <p>For example, if I do:</p>
  <p><code>repository.save(profileEntity)</code> this will return a newly persisted <code>ProfileEntity</code>, but now I need to map it to <code>ProfileDto</code> in order to make it part of <code>UserDto</code> before mapping again <code>UserDto</code> to <code>UserEntity</code> and persisting.</p>
  <p>Note: Dto here are objects I am planning to use as a response to the client (with JSON related annotations). These live in the <code>Service</code> and <code>Controller</code> layers, whereas Entity are JPA related objects that only live in the <code>Repository</code> and <code>Service</code> layers.</p>