title: Pattern for JPA: Generating Data Transfer Object DTO from Entity and merging DTO to database
body: <p>I am looking for a good way to create Data Transfer Objects (DTO) from a JPA Entity and vice versa.
  I want to send the DTO as JSON to a client, then receive the modified DTO and save it back to the database.
  It would be the most ease to perform the merge method from the EntityManager on the received object after it has been parsed from JSON to it's Java class. </p>
  
  <p>For example there is the following Entity and the Rest method for saving the modified object:</p>
  
  <hr>
  
  <pre><code>@Entity
  @Table(name="CUSTOMER")
  public class Customer {
      @Id
      Long id;
      @Version
      Long version;
      String name;
      String address;
      String login;
      String password;
      String creditCardNumber;
      @OneToMany(cascade = CascadeType.ALL)
      List&lt;Foo&gt; fooList;
  
      ... Getter() and Setter()
  }
  </code></pre>
  
  <hr>
  
  <pre><code>private EntityManager em;
  @POST
  @Path("/saveCustomer")
  public void saveCustomer ( Customer  customer)   {               
     em.merge(customer);
     return;
  }  
  </code></pre>
  
  <p>This works fine as long as I send the whole Entity Class as JSON and receive the whole Entity back. Then the EntityManager will merge the modified object to the database. But when I only want to provide a subset of the Entity (like only name and address of the customer) there will be problems:</p>
  
  <ol>
  <li><p>What would be the best way to create a subset of an Entity?</p>
  
  <p>    -Writing the DTOs for the Entity by hand? This will generate duplicate code for every subset of the entity, which has to be maintained.</p></li>
  <li><p>How to merge a DTO which is a subset of an Entity, back to the database?</p>
  
  <p>    -Using the merge() method of the EntityManager doesn't work. At first the DTO is no entity hence it can't be merged. And Just creating an Entity from the DTO, will have some unset values in the Entity. After a merge the values will be NULL in the database.</p></li>
  </ol>
  
  <hr>
  
  <p>One idea I came up with, was to specify additional Entities for each subset I want to have for an entity. (Like a database view) This would be duplicate code, but it could solve the problem with the merging of the DTO to the database. (And maybe this code can be auto-generated)</p>
  
  <p>For example the Entity CustomerView1 links to the same table as the Customer class, but only provides the name and address of the customer.  It's a DTO for the real Customer class, which can be sent as JSON and modified outside of the server. This class can then also be merged to the database by the EntityManager.</p>
  
  <pre><code>@Entity
  @Table(name="CUSTOMER")
  public class CustomerView1 {
      @Id
      Long id;
      @Version
      Long version;
      String name;
      String address;
      
          ... Getter() and Setter()
  }    
  </code></pre>
  
  <p>But I have doubts about this solution, I don't know if this will mess with the JPA's caching of Entities and may cause some problems. 
      </p>
  
  <hr>
  
  <p>My Question is, is there a pattern to solve code duplication for DTOs and merging the DTOs back to the database?</p>
  
  <p>Or is there a library for this purpose? - Something, like auto-generation for the DTOs and copying the DTOs back to the real Entity, to make it possible to merge them with the EntityManager.</p>