Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping value objects with Dozer

I'm using Dozer to map my DTOs to JPA entities.

One of the use-cases is that a DTO representation of an already existing entity arrives on a WS, then I find the entity using JPA and use Dozer to map the DTO on the found entity using map(source, destination) way of mapping (not the map(source, destinationClass)).

I have some value objects (with the classic immutable value object semantics) on my entities (such as Address) as @Embeddables. The thing is, that I want dozer to always create a new Address instance when setting it on e.g.: Employee object, and not map on the already existing Address instance.

So with the following classes:

public class Employee {

    private Address address;

    public void setAddress(Address address) {
        this.address = address;
    }

    public Address getAddress() {
        return this.address;
    }

}

I want dozer to call setAddress() always with a new Address instance and not by trying to map the new Address' fields with getAddress().

Is there any way to do this?

like image 319
nihilist84 Avatar asked Oct 24 '22 16:10

nihilist84


1 Answers

I think you could do this with a custom converter. See the section on custom converters in the dozer documentation.

like image 59
mark-cs Avatar answered Oct 27 '22 11:10

mark-cs