jmapper-framework / jmapper-core

Elegance, high performance and robustness all in one java bean mapper
http://jmapper-framework.github.io/jmapper-core
Apache License 2.0
227 stars 41 forks source link

[Question] xml mapping #72

Closed tokazio closed 7 years ago

tokazio commented 7 years ago

Name:

private String first;
private String last;
//getter/setter

Person:

private Name name;
//getter/setter

PersonDto:

private String firstName;
private String lastName;
//getter/setter

How to map Person to PersonDto with XML ?

name.first -> firstName name.last -> lastName

This doesn't work:

<conversion name="firstname" to="firstName" type="DYNAMIC">
    return ${source}.getName().getFirst();
</conversion>
avurro commented 7 years ago

Can you add mapping code and the error generated?

tokazio commented 7 years ago

Full xml config:

<?xml version="1.0" encoding="UTF-8"?>
<jmapper
    xmlns="https://jmapper-framework.googlecode.com"
    xmlns:xsi="https://jmapper-framework.googlecode.com/svn"
    xsi:noNamespaceSchemaLocation="jmapper.xsd">    
    <class name="net.esyo.mappersperfs.PersonDto">
        <attribute name="birthDate">
            <value name="birthDate"/>
        </attribute>
        <attribute name="aliasesList">
            <value name="knownAliases"/>
        </attribute>
        <conversion name="firstname" to="firstName" type="DYNAMIC">
            return ${source}.getName().getFirst();
        </conversion>
    </class>    
</jmapper>

Testing it with Junit and assertJ. Direct mapped fields are ok.

No stack, but PersonDto.firstName is null

Test trace:

expected:<"Romain"> but was: org.junit.ComparisonFailure at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at TestsPerfs.testJMapper(TestsPerfs.java:95) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:45) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

tokazio commented 7 years ago

I don't know if

<conversion name="firstname" to="firstName" type="DYNAMIC">
    return ${source}.getName().getFirst();
</conversion>

Is the right config to map Person.name.first to PersonDto.firstName

tokazio commented 7 years ago

I'm comparing bean mapping framework. You can get the project here

avurro commented 7 years ago

The mapping is not applied because the field "firstName" is not configured. Between attributes there isn't a field "firstName" mapped, so the conversion is ignored

tokazio commented 7 years ago

ha ok! i understand now... thanks! this one works, and it's fast ;)

<?xml version="1.0" encoding="UTF-8"?>
<jmapper
    xmlns="https://jmapper-framework.googlecode.com"
    xmlns:xsi="https://jmapper-framework.googlecode.com/svn"
    xsi:noNamespaceSchemaLocation="jmapper.xsd">    
    <class name="net.esyo.mappersperfs.PersonDto">
        <attribute name="birthDate">
            <value name="birthDate"/>
        </attribute>
        <attribute name="aliasesList">
            <value name="knownAliases"/>
        </attribute>
        <attribute name="firstName">
            <value name="name"/>
        </attribute>
        <conversion name="name.first" to="firstName" type="DYNAMIC">
            return ${source}.getFirst();
        </conversion>
        <attribute name="lastName">
            <value name="name"/>
        </attribute>
        <conversion name="name.last" to="lastName" type="DYNAMIC">
            return ${source}.getLast();
        </conversion>
    </class>    
</jmapper>