简体   繁体   中英

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 eg: 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM