0

I am using Dozer to do object Mapping. Everything works reaaly well just that I'm not able to map this particular thing.

<mapping>
<class-a>User</class-a>
<class-b>UAUserBean</class-b>
<field>
<a>RightLst.Right</a>
<b>Rights</b>
<a-hint>Right</a-hint>
<b-hint>UARightBean</b-hint>
</field>
<field>
<a>RightLst.NumInLst</a> 
<b>Rights.length</b> 
</field>
</mapping>

//here RightLst is an object of a class and numInLst (int prop)
//rights is an array of objects

what i want to do is

lUser.getRightLst().setNumInLst(uaUserBean.getRights().length);

Any suggestions??

Thanks in advance.


User{ 
protected RightLst rightLst;
}

RightLst{
protected Integer numInLst;
protected Collection right = new ArrayList();
}

public class UAUserBean{
private UARightBean[] rights;
}
Afaque
  • 129
  • 3
  • 15

1 Answers1

1

When you do this:

...
   <b>rights.length</b> 
</field>

Dozer will try to access the first position of the rights array and call the getter for the length property on an instance of UARightBean (which is the type of the array), obviously the length property doesn't exist in UARightBean and Dozer will throw an Exception.

I suggest to create a getter method in UAUserBean to return the length of the rights property, it would look like this:

 class UAUserBean {
        ... 
        public int getRightsLength() {
            return rights != null ? rights.length : 0;
        }       
    }

Mapping file:

<class-a>User</class-a>
<class-b>UAUserBean</class-b>
<field>
    <a>rightLst.numInLst</a> 
    <b>rightsLength</b> 
</field>

If you can't modify UAUserBean, your last option would be a custom converter from UARightBean[] to Integer, but it would look ugly.

davidmontoyago
  • 1,834
  • 14
  • 18
  • i don't have the right to modify UAUserBean.. I've already implemented using custom converters... but its true its ugly.. i was just wondering if rights was a collection. Would it be possible to call its size() function to map it to the field in the User class.. – Afaque Aug 07 '12 at 16:13
  • You will experience the same issue with a List field. – davidmontoyago Aug 07 '12 at 17:06