How to mapp an array to a complex type using Dozer

Go To StackoverFlow.com

0

I am using Dozer to map some beans, and I have a mapping that I can't figure out.

Here are my classes:

class A{
    private ComplexeType type;
    //Constructors & getters    
}

class B{
    private String[] type;
    //Constructors & getters 
}

class ComplexteType{
     private List<String> list;
    //Getter for the list, no constructor
}

How do I map class A to class B?

I want to map the field type of class A to the field type of the class B using xml.

here is the xml file:

<mapping>
        <class-a>A</class-a>
        <class-b>B</class-b>
        <field custom-converter="AToBCustomConverter">
            <a>type</a>
            <b>type</b>
        </field>
    </mapping>

And here is a sippet from my CustomConverter

if (source == null) {
            return null;
        }
        B dest = null;
        if (source instanceof java.lang.String) {
            // check to see if the object already exists
            if (destination == null) {
                dest = new A();
            } else {
                dest = (A) destination;
            }
            dest.getTypes().add((String) source);
            return dest;
        } else if (source instanceof B) {
            String[] sourceObj = ((B) destination)
                    .getType()
                    .toArray(
                            new String[((B) destination)
                                    .getType().size()]);
            return sourceObj;
        } else {
            throw new MappingException(
                    "Converter StatResultCustomConverter used incorrectly. Arguments passed in were:"
                            + destination + " and " + source);
        }
    }
2012-04-04 17:30
by Otmane MALIH
Please add a better description of your problem. Do you want to map the field list in A.type to B.type? Have you attempted anything so far? Are you mapping using XML, annotations or programatically - darrengorman 2012-04-04 17:35
what kind of output/error are you receiving from Dozer? can't get it done doesn't sound quite descriptiv - Alonso Dominguez 2012-04-04 17:37
I edited my question to put the xml mapping file and my customConverter class - Otmane MALIH 2012-04-04 17:52
This one answerd my question.

http://stackoverflow.com/questions/3018006/dozer-deep-mapping-not-workin - Otmane MALIH 2012-04-05 15:46



1

Here is the mapping I used to solve the problem.

<mapping>
  <class-a>Q</class-a>
  <class-b>B</class-b>   
  <field>
    <a is-accessible="true">type<list</a>
    <b is-accessible="true">type</b>
  </field>
</mapping>
2012-04-05 15:48
by Otmane MALIH


1

I don't think your CustomConverter is necessary in this case, see here.

Try this in your mapping file:

<mapping>
  <class-a>A</class-a>
  <class-b>B</class-b>
  <field>
    <a>type.list</a>
    <b>type</b>
  </field>
</mapping>

and Dozer should perform the nested mapping automatically.

2012-04-04 18:17
by darrengorman
I did that before but I get a null list - Otmane MALIH 2012-04-04 18:23
Does your application have logging enabled? Usually I find that Dozer mapping issues are related to incorrectly named getters/setters. Try and delve into the logs and see why the fields aren't being mapped as expected - darrengorman 2012-04-04 18:25
As I said in the question, the ComplexeType have only a getter for the List, there is no setter and no constructor, here is the comment of the getter: This accessor method returns a reference to the live list, * not a snapshot. Therefore any modification you make to the * returned list will be present inside the JAXB object. * This is why there is not a set method for the statResult property. * *

* For example, to add a new item, do as follows: *

     *    getStatResult().add(newItem);
     * 
Otmane MALIH 2012-04-05 09:09
I just found a similar question here http://stackoverflow.com/questions/3018006/dozer-deep-mapping-not-working . Can anyone help me - Otmane MALIH 2012-04-05 12:09
Ads