不平凡的推土机映射
我正在努力让 Dozer 屈服于我的意愿,做一些我觉得应该很简单的事情。我有两个相似的模型,我希望在它们之间进行映射,但是一个模型比另一个模型具有“更深”的层次结构,这在处理集合时给我带来了问题。考虑以下类:
源类:
class Foo {
String id;
NameGroup nameGroup;
// Setters/Getters
}
class NameGroup {
private List<Name> names;
// Setters/Getters
}
class Name {
private String nameValue;
// Setters/Getters
}
目标类:
class Bar {
private String barId;
private BarNames barNames;
// Setters/Getters
}
class BarNames {
private List<String> names;
// Setters/Getters
}
现在我想要以下单向映射:
Foo.id -> Bar.barId // Simple enough
但我随后需要:
Foo.nameGroup.names.nameValue -> Bar.barNames.names
因此 Foo.nameGroup.namesName
实例code> 应该会导致将 String
添加到 BarNames.names
列表中。这可能吗?
I'm struggling to get Dozer to bend to my will for something that I feel should be quite simple. I have two similar models that I wish to map between, however one has a 'deeper' hierarchy than the other and this is causing me problems when dealing with collections. Consider the following classes:
Source classes:
class Foo {
String id;
NameGroup nameGroup;
// Setters/Getters
}
class NameGroup {
private List<Name> names;
// Setters/Getters
}
class Name {
private String nameValue;
// Setters/Getters
}
Destination classes:
class Bar {
private String barId;
private BarNames barNames;
// Setters/Getters
}
class BarNames {
private List<String> names;
// Setters/Getters
}
Now I'd like the following one-way mappings:
Foo.id -> Bar.barId // Simple enough
But I then need:
Foo.nameGroup.names.nameValue -> Bar.barNames.names
So each Name
instance in Foo.nameGroup.names
should result in a String
being added to the BarNames.names
list. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要您的“Name”类包含 String 构造函数,就可以使用 Dozer 轻松完成此操作。
来自 Dozer 文档的引用 (http://dozer.sourceforge.net/documentation/simpleproperty.html< /a>):
我已经用上面的类测试了它(我遇到了同样的问题)并且它工作得很好。这是我使用的映射:
This can easily be done with Dozer as long as your "Name" class contains a String constructor.
A quote from the Dozer docs (http://dozer.sourceforge.net/documentation/simpleproperty.html):
I have tested this with your classes as above (I was stuck with the same problem) and it works perfectly. Here is the mapping I used: