Search code examples
javacollectionsmappingdozer

Dozer Mapping between primitive datatype and Custom object?


I have the following scenario :

Class A{
  private List<Long> longList;
  //getter and setter
}

Class B{
  private List<C> listC;
  //getter and setter
}

Class C{
  private Long id;
  //getter and setter
}

Now, I want to convert between longList and C. I found the following mapping :

<mapping>
  <class-a>A</class-a>
  <class-b>B</class-b>
  <field>
    <a>longList</a>
    <b>listC</b>
    <a-hint>java.lang.Long</a-hint>
    <b-hint>C</b-hint>
  </field>
</mapping>

I am not sure whether the above mapping is the proper solution or not. Can I set up a mapping between long and C , such that long gets mapped to C.id ?


Solution

  • You could try mapping Long to C like so:

    <mapping>
        <class-a>A</class-a>
        <class-b>B</class-b>
        <field>
            <a>longList</a>
            <b>listC</b>
        <field>
    </mapping>
    
    <mapping>
        <class-a>java.lang.Long</class-a>
        <class-b>C</class-b>
        <field>
            <a>this</a>
            <b>id</b>
        <field>
    </mapping>