0

I have an xml mapping defined:

<mapping>
    <class-a>java.util.HashMap</class-a>
    <class-b>com.example.MyClass</class-b>
    <field>
        <a key=&quot;myDateField&quot;>this</a>
        <b>myXMLGregorianCalendarField</b>
    </field>
</mapping>

Here value for key myDateField contains instance of java.lang.Date class. Field com.example.MyClass#myXMLGregorianCalendarField expects instance of javax.xml.datatype.XMLGregorianCalendar.

This mapping always throws an exception:

MapId: null
Type: null
Source parent class: java.util.HashMap
Source field name: this
Source field type: class java.util.Date
Source field value: Thu Jan 01 03:00:00 MSK 1970
Dest parent class: com.example.MyClass
Dest field name: myXMLGregorianCalendarField
Dest field type: javax.xml.datatype.XMLGregorianCalendar
org.dozer.MappingException: Illegal object type for the method 'setMyXMLGregorianCalendarField'. 
Expected types: 
    javax.xml.datatype.XMLGregorianCalendar
Actual types: 
    java.util.Date

How to make this conversion work properly?

Note Long-long debugging revealed that primitive converters are called differently for maps and "non-maps". So here comes the second question: why?

3 Answers3

1

I'm not sure what you mean in the last section but you can try using a custom setter in the destination class to perform this mapping.

Your mapping file would look like this:

<mapping>
    <class-a>java.util.HashMap</class-a>
    <class-b>com.example.MyClass</class-b>
    <field>
        <a key="myDateField">this</a>
        <b set-method="setMyXMLGregorianCalendarField(java.util.Date)">myXMLGregorianCalendarField</b>
    </field>
</mapping>

Implement the custom setter in MyClass, perhaps using a conversion like this.

Community
  • 1
  • 1
darrengorman
  • 12,952
  • 2
  • 23
  • 24
  • **1)** There is no method `com.example.MyClass#setMyXMLGregorianCalendarField(java.util.Date)` available. Only `com.example.MyClass#setMyXMLGregorianCalendarField(javax.xml.datatype.XMLGregorianCalendar)` exists. **2)** Dozer is shipped with `XMLGregorianCalendarConverter` which works fine if source object (defined by `` tag) is **not** any instance of `Map` – Savva Mikhalevski Apr 03 '12 at 15:28
  • 1
    I assumed that `MyClass` is your class. Your other option is to implement a custom converter extending the `DozerConverter` class as described [here](http://dozer.sourceforge.net/documentation/customconverter.html) – darrengorman Apr 03 '12 at 15:32
  • Of course implementing another custom converter is an easiest (and the most obvious) way to make this conversion, but I don't want to reinvent the wheel. Dozer already has such converter built-in, so I want to reuse it. Anyway, thanks for your help! – Savva Mikhalevski Apr 04 '12 at 08:59
  • I agree. It's a shame that the `XMLGregorianCalendarConverter` doesn't extend the `DozerConverter` class for such cases. Please accept my answer if it helped you :) – darrengorman Apr 04 '12 at 10:00
0

You can try the hint tag to implicitely convert from date to gregorian.

Here s a sample code:


    &ltfield>  
       &lta key="myDateField">this&lt/a>
       &ltb>myXMLGregorianCalendarField&lt/b>
       &lta-hint>java.util.GregorianCalendar&lt/a-hint>
   &lt/field>

 

I don't know whether dozer the implicit type conversion or not , but if it does , then you don't need to write any exta custom convertor method. In case , it does not perform an implicite conversion, try custom getter or setter method. in which perform the date to GregorianCalendar conversion. See this for custom getter and setter methods : custom getter-setter

Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
  • I checked it recently. It does implicite conversion for date to xmlgregoriancalander. So i think ,you want required any custom converter. – Priyank Doshi May 21 '12 at 12:06
0

Oops, I found the answer here, Automatic conversion in dozer

Under this,look at third last option in Data type conversion heading. They wrote these can be mapped internally without any custom convertor help : java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp, java.util.Calendar, java.util.GregorianCalendar

Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82