title: DTO pattern: Best way to copy properties between two objects
body: <p>In my application's architecture I usually send the object or list of objects from the data access layer to the web layer via the service layer, in which these objects get transformed from a <a href="http://en.wikipedia.org/wiki/Data_access_object" rel="noreferrer">DAO</a> object to a <a href="https://en.wikipedia.org/wiki/Data_transfer_object" rel="noreferrer">DTO</a> object and vice versa. The web layer don't have any access to DAO objects and the DAO layer do not use DTOs.</p>
  
  <p>To demonstrate, I usually write the code as:</p>
  
  <pre><code>@Transactional(readOnly = true)
  public List&lt;UserDTO&gt; getAllUserAsUserDTO() {
      List&lt;UserDTO&gt; userDTOs = new ArrayList&lt;UserDTO&gt;();
  
      for(User user : getAllUser()) {
          userDTOs.add(constructUserDTO(user));
      }
  
      return userDTOs;
  }
  
  private UserDTO constructUserDTO(User user) {
      UserDTO userDTO = new UserDTO();
      userDTO.setFullName(user.getFullName());
      userDTO.setId(user.getId());
      userDTO.setUsername(user.getUsername());
      userDTO.setRole(user.getRole());
      userDTO.setActive(user.isActive());
      userDTO.setActiveText(user.isActive() ? "Active" : "Inactive");
      return userDTO;
  }
  </code></pre>
  
  <p>Here the user is the database entity:</p>
  
  <pre><code>@javax.persistence.Entity
  @Table(name = "USER")
  public class User extends Entity {
  
      @Transient
      private static final long serialVersionUID = -112950002831333869L;
  
      private String username;
      private String fullName;
      private boolean active;
      private String role;
      // other fields
  
      public User() {
          super();
      }
  
      @NaturalId
      @Column(name = "USERNAME", nullable = false)
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      @Column(name = "FULL_NAME")
      public String getFullName() {
          return fullName;
      }
  
      public void setFullName(String fullName) {
          this.fullName = fullName;
      }
  
      @Column(name = "ACTIVE", nullable = false)
      public boolean isActive() {
          return active;
      }
  
      public void setActive(boolean active) {
          this.active = active;
      }
  
      @Column(name = "ROLE")
      public String getRole() {
          return role;
      }
  
      public void setRole(String role) {
          this.role = role;
      }
  }
  </code></pre>
  
  <p>And this is the UserDTO:</p>
  
  <pre><code>public class UserDTO extends BaseDTO {
  
      private static final long serialVersionUID = -3719463614753533782L;
  
      private String username;
      private String fullName;
      private String role;
      private String activeText;
      private Boolean active;
      //other properties
  
      public UserDTO() {
          super();
      }
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getFullName() {
          return fullName;
      }
  
      public void setFullName(String fullName) {
          this.fullName = fullName;
      }
  
      public String getRole() {
          return role;
      }
  
      public void setRole(String role) {
          this.role = role;
      }
  
      public String getActiveText() {
          return activeText;
      }
  
      public void setActiveText(String activeText) {
          this.activeText = activeText;
      }
  
      public Boolean getActive() {
          return active;
      }
  
      public void setActive(Boolean active) {
          this.active = active;
      }
  }
  </code></pre>
  
  <p>So I was wondering if this is the only way to copy properties between two objects. I guess I am not sure. Also I am using <a href="http://code.google.com/p/lambdaj/" rel="noreferrer">lambdaj</a>, so is there a method in this API by which I can copy all these properties to create list of other objects?</p>
  
  <p>This topic may be sounds subjective, but I really want to know from you experts the ways by which the transformation of object from one form to another can be done where the maximum fields are having same string.</p>