title: DTO mapping and updating to a Database with Java, Dozer and Hibernate
body: <p>I'm using Dozer to map Hibernate entities to their DTOs. A simplified example class is the following:</p>
  
  <pre><code>@Entity
  public class Role {
  
       @Id
       @GeneratedValue
       @Column(name="RoleId", nullable=false)
       public int roleId;
  
       @Column(name="RoleName", nullable=false)
       @NotEmpty
       public String roleName;
  
       //get + set  (not for the roleId since its autogenerated)
  }
  </code></pre>
  
  <hr>
  
  <pre><code>   public class RoleDTO {   
  
       private int roleId;
       private String roleName;
  
       public RoleDTO(int roleId, String roleName) {
            this.roleId = roleId;
            this.roleName = roleName;
       }
  
       public RoleDTO() {}
  
       //gets + sets
    }
  </code></pre>
  
  <p>Now the mapping works fine, but I have a problem when trying to make an Update.  Let's say I have a Role (1,"Administrator") in my database.  My view first generates the DTO with the updated fields:</p>
  
  <pre><code>RoleDTO roleDTO = new RoleDTO(1, "admin");
  </code></pre>
  
  <p>Eventually the class that persists the Role receives the DTO and transforms it to an Entity class through Dozer to persist the changes:</p>
  
  <pre><code>Role role = DozerMapper.map(roleDTO,Role.class);    
  </code></pre>
  
  <p>At this point, my role entity has lost its ID, presumably because the ID column is defined as auto-increment, and I obviously can't update the null-ID entity.</p>
  
  <p>So how should I approach this problem so that the ID and the updated fields get all mapped to the entity?  I could always bring the entity object with hibernate and update each of its fields with the ones from the DTO and save it back, but it would defeat the whole purpose of using Dozer.</p>
  
  <p>Thanks for any help.</p>