0

我正在使用 Dozer 进行对象映射。一切都很好,只是我无法映射这个特定的东西。

<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

我想做的是

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

有什么建议么??

提前致谢。


User{ 
protected RightLst rightLst;
}

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

public class UAUserBean{
private UARightBean[] rights;
}
4

1 回答 1

1

当你这样做时:

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

Dozer 将尝试访问数组的第一个位置,并在(这是数组的类型)的实例上调用lengthrights属性的 getter ,显然length属性不存在并且 Dozer 会抛出异常。UARightBeanUARightBean

我建议创建一个 getter 方法UAUserBean来返回rights属性的长度,它看起来像这样:

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

映射文件:

<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.

于 2012-08-07T16:05:33.650 回答