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);
        }
    }
Otmane MALIH
  • 41
  • 2
  • 10
  • 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 Apr 04 '12 at 17:35
  • what kind of output/error are you receiving from Dozer? can't get it done doesn't sound quite descriptive – Alonso Dominguez Apr 04 '12 at 17:37
  • I edited my question to put the xml mapping file and my customConverter class. – Otmane MALIH Apr 04 '12 at 17:52
  • This one answerd my question. http://stackoverflow.com/questions/3018006/dozer-deep-mapping-not-working – Otmane MALIH Apr 05 '12 at 15:46

2 Answers2

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.

darrengorman
  • 12,952
  • 2
  • 23
  • 24
  • I did that before but I get a null list. – Otmane MALIH Apr 04 '12 at 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 Apr 04 '12 at 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 Apr 05 '12 at 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 Apr 05 '12 at 12:09
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>
Jayan
  • 18,003
  • 15
  • 89
  • 143
Otmane MALIH
  • 41
  • 2
  • 10