简体   繁体   English

使用推土机映射值对象

[英]Mapping value objects with Dozer

I'm using Dozer to map my DTOs to JPA entities. 我正在使用Dozer将我的DTO映射到JPA实体。

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) ). 其中一个用例是一个已经存在的实体的DTO表示到达一个WS,然后我使用JPA找到实体,并使用推土机使用地图(源,目的地)的映射方式向DTO地图发现的实体(不是map(source,destinationClass )。

I have some value objects (with the classic immutable value object semantics) on my entities (such as Address) as @Embeddables. 我在实体(例如Address)上有一些值为@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. 问题是,我希望推土机在将其设置在例如Employee对象上时始终创建一个新的 Address实例,而不要映射到已经存在的Address实例上。

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(). 我希望推土机始终使用新的Address实例来调用setAddress(),而不是尝试通过getAddress()映射新的Address'字段。

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. 请参阅推土机文档中有关自定义转换器的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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