Runtime errors when using multiple converters and deep mapping in modelmapper 0.7.2

2,082 views
Skip to first unread message

Jonathan L

unread,
Jul 17, 2014, 10:13:50 AM7/17/14
to model...@googlegroups.com
modelmapper version: 0.7.2
Java: Windows Java7u60

Issue 1: Deep mapping using the same source object

All my classes are JavaBeans(TM) in this example with all the usual getter, setters and default constructor.

    private static class CreditSecurityContractualCashflowPropertyMap
        extends PropertyMap<CreditSecurityContractualCashflow, CreditSecurityContractualCashflowDto>
    {
        @Override
        protected void configure()
        {
            map(source).setCreditSecurityId(source.getParentEntityIdentity().getId());
            map(source).setCreditSecurityTemporalId(source.getParentEntityIdentity().getVersionGuid());
        }
    }

When modelmapper validates the above, it complains with the following:

org.modelmapper.ConfigurationException: ModelMapper configuration errors:

1) A mapping is missing a required destination member.

2) Invalid source method com.trafigura.cashsettlement.model.CreditSecurityContractualCashflow.getParentEntityIdentity(). Ensure that method has zero parameters and does not return void.

3) Invalid source method com.trafigura.cashsettlement.model.CreditSecurityParentEntityIdentity.getId(). Ensure that method has zero parameters and does not return void.

4) A mapping is missing a required destination member.

5) Invalid source method com.trafigura.cashsettlement.model.CreditSecurityContractualCashflow.getParentEntityIdentity(). Ensure that method has zero parameters and does not return void.



Issue 2: Multiple converters

To sidestep the deep mapping issues above, I tried to install my own converters.

    private static class CreditSecurityContractualCashflowPropertyMap
        extends PropertyMap<CreditSecurityContractualCashflow, CreditSecurityContractualCashflowDto>
    {
        @Override
        protected void configure()
        {
            super.configure();
            final Converter<CreditSecurityContractualCashflow, Long> longConverter = new AbstractConverter<CreditSecurityContractualCashflow, Long>() {
                @Override
                protected Long convert(CreditSecurityContractualCashflow source)
                {
                    return source.getParentEntityIdentity() != null ? source.getParentEntityIdentity().getId() : null;
                }
            };
            final Converter<CreditSecurityContractualCashflow, Guid> guidConverter = new AbstractConverter<CreditSecurityContractualCashflow, Guid>() {
                @Override
                protected Guid convert(CreditSecurityContractualCashflow source)
                {
                    return source.getParentEntityIdentity() != null ? source.getParentEntityIdentity().getVersionGuid() : null;
                }
            };
            using(longConverter).map(source).setCreditSecurityId(/* Long date type  */ null);
            using(guidConverter).map(source).setCreditSecurityTemporalId(/* Guid data type */ null);
        }
    }

In this case, modelmapper attempted to wedge a Long into setCreditSecurityTemporalId. Note that this is definitely not a Long setter. I encountered a runtime error where the wrong converter is used:

org.modelmapper.MappingException: ModelMapper mapping errors:

1) Error mapping com.trafigura.cashsettlement.model.CreditSecurityContractualCashflow to com.trafigura.cashsettlement.api.dto.contractual.CreditSecurityContractualCashflowDto

1 error
at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:374)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:69)
at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:497)
at org.modelmapper.ModelMapper.map(ModelMapper.java:340)
at com.trafigura.cashsettlement.api.mapping.ObjectMapper.map(ObjectMapper.java:48)
at com.trafigura.cashsettlement.api.mapping.ContractualCashflowDtoMappingTest.credit_security(ContractualCashflowDtoMappingTest.java:218)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:701)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:893)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1218)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:758)
at org.testng.TestRunner.run(TestRunner.java:613)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1170)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1095)
at org.testng.TestNG.run(TestNG.java:1007)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:121)
Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:

1) Failed to set value '1' on com.trafigura.cashsettlement.api.dto.contractual.CreditSecurityContractualCashflowDto.setCreditSecurityTemporalId()

1 error
at org.modelmapper.internal.Errors.toMappingException(Errors.java:258)
at org.modelmapper.internal.PropertyInfoImpl$MethodMutator.setValue(PropertyInfoImpl.java:118)
at org.modelmapper.internal.MappingEngineImpl.setDestinationValue(MappingEngineImpl.java:249)
at org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:180)
at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:131)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:92)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:60)
... 34 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.modelmapper.internal.PropertyInfoImpl$MethodMutator.setValue(PropertyInfoImpl.java:116)
... 39 more



Am I doing anything wrong here or is this a bug with the latest version? I didn't notice these kind of issues in version 0.6.5.

Thanks
Jonathan

Jonathan L

unread,
Jul 17, 2014, 10:39:34 AM7/17/14
to model...@googlegroups.com
Correction for first example. Should have been

    public static class CreditSecurityContractualCashflowPropertyMap
        extends PropertyMap<CreditSecurityContractualCashflow, CreditSecurityContractualCashflowDto>
    {
        @Override
        protected void configure()
        {
            map().setCreditSecurityId(source.getParentEntityIdentity().getId());
            map().setCreditSecurityTemporalId(source.getParentEntityIdentity().getVersionGuid());
Reply all
Reply to author
Forward
0 new messages