Hi,
First of all, I am just starting to use modelmapper.
1) MemberDetails.java ( This class was generated from WSDL via wsimport ). This is the source. It has a few properties but the one I am having problems with is the property of type XMLGregorianCalendar, as below:
public class MemberDetails {
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar birthDate;
public XMLGregorianCalendar getBirthDate() {
return birthDate;
}
public void setBirthDate(XMLGregorianCalendar value) {
this.birthDate = value;
}
}
2) MemberDTO.java. This is the destination:
@ManagedBean
@ViewScoped
public class MemberDTO {
public Date birthDate;
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
A) If I do the following:
ModelMapper mapper = new ModelMapper();
member = mapper.map(memberDetails, MemberDTO.class);
I get:
1) Converter org.modelmapper.internal.converter.NumberConverter@1892c59 failed to convert javax.xml.datatype.XMLGregorianCalendar to int.
1 error
at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:338)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:77)
at org.modelmapper.ModelMapper.map(ModelMapper.java:185)
at au.aas.com.bpm.insurance.InsuranceCase.retrieveMemberDetails(InsuranceCase.java:157)
at au.aas.com.bpm.insurance.InsuranceCase.main(InsuranceCase.java:164)
Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:
1) Error mapping 1970-05-31+10:00 to java.lang.Integer
It seems to be mapping the source property MemberDetails.birthDate to the destination property MemberDTO.birthDate.date, which is not what I was expecting ( notice the extra .date ). I was expecting it to map the source property MemberDetails.birthDate to the destination property MemberDTO.birthDate.
B) So I created a new Map:
public class MemberMap extends PropertyMap<MemberDetails, MemberDTO> {
@Override
protected void configure() {
map().setBirthDate(source.getBirthDate().toGregorianCalendar().getTime());
}
}
.. and now the following code:
ModelMapper mapper = new ModelMapper();
mapper.addMappings(new MemberMap());
member = mapper.map(memberDetails, MemberDTO.class);
.. results in:
Exception in thread "main" org.modelmapper.ConfigurationException: ModelMapper configuration errors:
1) Invalid source method java.util.Calendar.setTimeInMillis(). Ensure that method has zero parameters and does not return void.
1 error
at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:224)
at org.modelmapper.internal.MappingBuilderImpl.build(MappingBuilderImpl.java:138)
at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:66)
at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:93)
at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:90)
C) I can use the STRICT matching strategy, but then I loose the matching for the other properties which was working with the STANDARD matching strategy.
D) I have also tried adding a converter to the mapper:
ModelMapper mapper = new ModelMapper();
mapper.addConverter(new XMLGregorianCalendarToDateConverter());
.. but that did not work as well as it thinks that the source MemberDetails.birthDate matches to the destination MemberDTO.birthDate.date, as per [A].
Any ideas ?
Thanks.