Search code examples
javadozer

How to use Dozer to copy data between classes without destroying the target class


Using the example bellow:

public void reload(WorkTemplateDTO workTarget) throws Exception  {
    WorkTemplateDTO work = this.load(data.getId());
    workTarget= mapper.map(work, WorkTemplateDTO.class);
}

The instance 'workTarget' received as argument and destination of the copy is being replaced by a new instance with data from 'work'.

I would like to know if is possible to use Dozer to just copy data from source (work) to destination (workTarget) without destroing the old instance of 'workTarget'.

Tks!


Solution

  • Dozer also allows object to object mapping, so you can use the mapper in following way

    public void reload(WorkTemplateDTO workTarget) throws Exception  {
        WorkTemplateDTO work = this.load(data.getId());
        mapper.map(work, workTarget);
    }
    

    ref: DozerBeanMapper