集合映射
假设我有以下类:
class A1 {
List<B1> bList;
}
class B1 {
Long id;
}
---
class A2 {
List<Long> bList;
}
我想使用 Dozer 将类 A1 映射到 A2,其中 A1.bList 包含 B1 对象,A2.bList 仅包含 B1 对象的 ID。
映射会是什么样子?
谢谢。
Suppose I have the following classes:
class A1 {
List<B1> bList;
}
class B1 {
Long id;
}
---
class A2 {
List<Long> bList;
}
I want to map class A1 to A2 with Dozer, where A1.bList contains B1 objects and A2.bList contains only the IDs of the B1 objects.
How would the mapping look like?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您可以尝试设置
Long
到B1
的映射。如果我没记错的话,这只能以一种方式起作用,但我不记得是哪种方式。抱歉,希望这有帮助。I think you could try setting up a mapping for
Long
toB1
. If I remember rightly this only works one way, I can't remember which way tho. Sorry, hope this helps.您可以使用推土机定制转换器。 Dozer 客户转换器
:(可能有错误,没有编译或测试)
示例 自定义转换器:
You can use a dozer custom converter. Dozer customer converter
An example: (possible errors, didn't compile or test it)
The custom converter:
我认为你可以通过覆盖 B1 中的 toString() 方法来做到这一点,它会起作用。
下面是示例代码:
在您的映射中进行以下更改:
因此,当 dozer 尝试绑定 B1 时,它将返回其 id 作为 String,然后 dozer 将在 String 和 Long 之间执行自动转换。
I think you can do this by overridding toString() method in B1 and it would work.
Here's sample code:
And in your mapping do the following changes:
So,when dozer tries to bind B1, it will return its id as String and then dozer will perform an automatic conversion between String and Long.