Savva Mikhalevski
Savva Mikhalevski

Reputation: 327

Dozer mapping of java.lang.Date (as a Map field) to XMLGregorianCalendar

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?

Upvotes: 0

Views: 7161

Answers (3)

Priyank Doshi
Priyank Doshi

Reputation: 13161

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

Upvotes: 0

Priyank Doshi
Priyank Doshi

Reputation: 13161

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

Here s a sample code:


    <field>  
       <a key="myDateField">this</a>
       <b>myXMLGregorianCalendarField</b>
       <a-hint>java.util.GregorianCalendar</a-hint>
   </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

Upvotes: 0

darrengorman
darrengorman

Reputation: 13114

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.

Upvotes: 1

Related Questions