使用 Dozer 映射值对象
我正在使用 Dozer 将 DTO 映射到 JPA 实体。
用例之一是现有实体的 DTO 表示到达 WS,然后我使用 JPA 找到该实体,并使用 Dozer 将 DTO 映射到找到的实体上。 map(source,destination) 映射方式(不是map(source,destinationClass))。
我的实体(例如地址)上有一些值对象(具有经典的不可变值对象语义)作为@Embeddables。问题是,我希望推土机在将其设置为例如 Employee 对象时始终创建一个 new Address 实例,而不是映射到已经存在的 Address 实例。
因此,对于以下类:
public class Employee {
private Address address;
public void setAddress(Address address) {
this.address = address;
}
public Address getAddress() {
return this.address;
}
}
我希望推土机始终使用新的 Address 实例调用 setAddress(),而不是尝试使用 getAddress() 映射新的 Address' 字段。
有什么办法可以做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以使用自定义转换器来做到这一点。请参阅推土机文档中有关自定义转换器的部分。
I think you could do this with a custom converter. See the section on custom converters in the dozer documentation.