Parsing: Entity Classes to DTO I'm building a GWT application for GAE, using JDO for Datastore access. I use and Entity class for data mapping, so it contains lots of Persistence annotations:

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class AppointmentEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    @Persistent
    private String firstName;
    @Persistent
    private String lastName;
    @Persistent
    private String DNI;
    @Persistent
    private Date appointmentDate;
    @Persistent
    private String attendant;
    @Persistent
    private String treatment;
    @Persistent
    private String details;
    @Persistent
    private Date nextAppointment;
    @Persistent(serialized = "true")
    private DownloadableFile file;

    //getters and setters...


Because of GWT and JDO nature, i can't use this Entity classes in my presentation layer; so there's a need of passing Entity Object attributes to the corresponding DTO, which is a POJO that has the same attributes of the Entity class:

public class AppointmentDTO implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private String firstName;
    private String lastName;
    private String DNI;
    private Date appointmentDate;
    private Date nextAppointmentDate;
    private String attendant;
    private String treatment;
    private String details;

    //getters and setters...


To do the Job of mapping attributes i make a Wrapper class that handles it, but it doesn't feel quite right to me. Does anyone have a better approach?

public class AppointmentWrapper {

    private List&lt;AppointmentEntity&gt; entitiesList;

    public AppointmentWrapper(List&lt;AppointmentEntity&gt; appointmentEntities) {
            entitiesList = appointmentEntities;
    }

    public List&lt;AppointmentDTO&gt; getDTOList() {
            List&lt;AppointmentDTO&gt; dtoList = new ArrayList&lt;AppointmentDTO&gt;();

            for (AppointmentEntity entity : entitiesList) {
                    AppointmentDTO dto = new AppointmentDTO();

                    dto.setId(entity.getId());
                    dto.setFirstName(entity.getFirstName());
                    dto.setLastName(entity.getLastName());
                    dto.setAppointmentDate(entity.getAppointmentDate());
                    dto.setNextAppointmentDate(entity.getNextAppointment());
                    dto.setAttendant(entity.getAttendant());
                    dto.setDetails(entity.getDetails());
                    dto.setDNI(entity.getDNI());
                    dto.setTreatment(entity.getTreatment());
                    dtoList.add(dto);
            }
            return dtoList;
    }
}
There is this concerning GWT and GAE objects. If that doesn't work for you, I would rather have a class of public static methods that does the conversions rather than sticking it into some kind of "wrapper class" that contains object states. For example,

public final class JDOEntityUtil {

   static public AppointmentDTO toDTO(AppointmentEntity entity) {
       AppointmentDTO dto = new AppointmentDTO();
       dto.setId(entity.getId());
       dto.setFirstName(entity.getFirstName());
       dto.setLastName(entity.getLastName());
       dto.setAppointmentDate(entity.getAppointmentDate());
       dto.setNextAppointmentDate(entity.getNextAppointment());
       dto.setAttendant(entity.getAttendant());
       dto.setDetails(entity.getDetails());
       dto.setDNI(entity.getDNI());
       dto.setTreatment(entity.getTreatment());
       return dto;
   }

   static public AppointmentEntity toEntity(AppointmentDTO dto) {
       AppointmentEntity entity = new AppointmentEntity();
       entity.setId(dto.getId());  /* or however you want to construct this */
       entity.setFirstName(dto.getFirstName());
       entity.setLastName(dto.getLastName());
       entity.setAppointmentDate(dto.getAppointmentDate());
       entity.setNextAppointmentDate(dto.getNextAppointment());
       entity.setAttendant(dto.getAttendant());
       entity.setDetails(dto.getDetails());
       entity.setDNI(dto.getDNI());
       entity.setTreatment(dto.getTreatment());
       return entity;
   }
}