0

I'm using dozer to map between my Model Entities and my DTOs. Now I'm facing with the problem that I need to map some properties of classA.classC to different properties of classB, but first I need to check for inconsistency, because if I don't classC will throws exception and the mapping will not work.

So assume that I have:

class ClassA {

   private String name;
   private ClassC c;


   public ClassC getC() throws ValidityException; 
}

class ClassB {

   private String code;
   private Integer value;

}

class ClassC {

   private String name;
   private Integer value;

   // Getters & Setters below
}

So now I want to map like this:

<mapping>
   <class-a>ClassA</class-a>
   <class-b>ClassB</class-b>
   <field>
      <a>c.name</a>
      <b>code</b>
   </field>
   <field>
      <a>c.value</a>
      <b>value</b>
   </field>
 </mapping>

if access to ClassC instance from ClassA instance throws exception, I will need to map null for both b properties. From what I was reading I assume that I should use a CustomConverter in order to access ClassC instance catch the exception and map null in that cases, but not sure how can I implement this kind of converter.

Anyone could give me some ideas about how this can be implemented using Dozer?

Daniel Ardison
  • 502
  • 4
  • 18

1 Answers1

0

Are you sure you wrote the correct mapping? Because ,

<field>
  <a>c.name</a>
  <b>name</b>

In above snippet, you wrote name for classB. Actually it should be code.

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