推土机映射JodaTime属性不按预期工作 [英] Dozer mapping JodaTime property not working as expected
问题描述
我正在使用Dozer在Document类和DocumentManagementBean类之间进行映射,这两者都是我自己制作的。两者都有Joda DateTime类型的getter和setter属性,名为dateAdded。
I am using Dozer to map between a Document class to DocumentManagementBean class, both of my own making. Both have a property, with getters and setters, of Joda DateTime type, called dateAdded.
当Document对象 d
具有属性 dateAdded
= x,调用 mapper.map(d,DocumentManagementBean.class)
所有字段都能正确自动映射(因为我可以完全控制代码库,我可以通过没有dozer-config而只依赖于匹配的属性名称),除了 dateAdded
字段,其中新的DocumentManagementBean dmb
最终在其 dateAdded
属性中使用当前 DateTime,而不是x来自 d
对象。
When Document object d
has property dateAdded
=x, calling mapper.map(d, DocumentManagementBean.class)
all fields get auto-mapped correctly (since I have full control over code base I am able to get away with no dozer-config and rely simply on matching property names), EXCEPT the dateAdded
field, where the new DocumentManagementBean dmb
ends up with the current DateTime in its dateAdded
property, instead of x from the d
object.
我期待Dozer试图打电话
I am expecting Dozer to try to call
dmb.setDateAdded(d.getDateAdded());
只需带上datefrom从源到目标,但它似乎是为dmb对象创建新的DateTime然后单独留下。
and just bring the value of dateAdded from source to target, but it seems to be creating new DateTime for dmb object an then leaving it alone.
有人可以为我解释这个吗?
Can anyone shed some light on this for me please?
推荐答案
基本问题是Dozer通过新的DateTime()创建一个新的DateTime空白实例,这就是你如何结束当前日期/时间而不是原始日期。可能有多种解决方案,我通常使用全局定义的customconverter:
The basic problem is that Dozer creates a new blank instance of DateTime, via new DateTime(), and thats how you end up with the current date/time instead of the original one. There might be multiple solutions, I usually went with a customconverter, globally defined:
<converter type="de.kba.resper.customconverter.DateTimeCustomConverter">
<class-a>org.joda.time.DateTime</class-a>
<class-b>org.joda.time.DateTime</class-b>
</converter>
和
public class DateTimeCustomConverter extends DozerConverter<DateTime, DateTime> {
public DateTimeCustomConverter() {
super(DateTime.class, DateTime.class);
}
@Override
public DateTime convertTo(final DateTime source, final DateTime destination) {
if (source == null) {
return null;
}
return new DateTime(source);
}
@Override
public DateTime convertFrom(final DateTime source, final DateTime destination) {
if (source == null) {
return null;
}
return new DateTime(source);
}
}
但它可能过头了: )
这篇关于推土机映射JodaTime属性不按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!