如何将集合中包含的对象映射到字段,反之亦然?推土机

时间:2017-02-14 18:25:45

标签: java dozer

我现在因为这个麻烦而奋斗了两天,我无法得到它。我有:

public class ClientBo{
  ...
  List<PersonBo> person;
  ...
}

public class ClientVo{
   ...
   PersonVo person;
   ...
}

我需要做的是以某种方式配置推土机,所以我可以从PersonBo列表映射到单个字段PersonVo(Vo an Bo具有相同的字段名称)。

Dozer有一个内置函数,可以从集合转换为单个字段,但不是另一种方式。 http://dozer.sourceforge.net/documentation/faq.html#mult-fields-to-single-field

我想出的唯一解决方案是:

<mapping type="one-way">
    <class-a>...ClientBo</class-a>
    <class-b>...ClientVo</class-b>
    <field>
        <a>person[0]</a>
        <b>person</b>
    </field>
</mapping>
<mapping type="one-way">
    <class-a>...ClientVo</class-a>
    <class-b>...ClientBo</class-b>
    <field custom-converter="mapper.CustomObjectToList">
        <a>person</a>
        <b>person</b>
    </field>
</mapping>

   public class CustomObjectToList implements CustomConverter{

        public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
            if(sourceFieldValue==null)
                return null;

            if(sourceFieldValue instanceof List && ((List<?>) sourceFieldValue).size()>0){
               /* This if is an attempt to get the first element of the
                list and return it as a single field, but id doesn't work*/
                Object o = ((List<?>)sourceFieldValue).get(0);
                return o;


            }else{
                /*Here a single field is put in a List and returned*/
                ArrayList<Object> result = new ArrayList<>();
                result.add(sourceFieldValue);
                return result;
            }
        }
    }

有什么方法可以删除

   <mapping type="one-way">
       <class-a>...ClientBo</class-a>
       <class-b>...ClientVo</class-b>
       <field>
            <a>person[0]</a>
            <b>person</b>
       </field>
   </mapping>

通过自定义转换器完成工作?它应该更通用,因此它可以适合类似的上下文。

谢谢!

1 个答案:

答案 0 :(得分:1)

<强> 1。您是否尝试过测试CustomConverter?

因为如果您尝试打印出任何变量,那么您将获得ClassCastException,如下所示:

Exception in thread "main" java.lang.ClassCastException: beans.PersonVo cannot be cast to beans.PersonBo
    at execute.Execute.main(Execute.java:37)

是的,具有相同名称的字段会自动映射,但在您的情况下,您使用CustomConverter进行映射。换句话说,Dozer相信你会照顾它,所以你不能简单地将一个Class变量(personVo)指向另一个不相关的Class实例(personBo)。

<强> 2。纠正上述例外

纠正上述异常的最简单方法是使用setter和getter方法,就像使用任何不相关的类一样:

public class CustomObjectToList implements CustomConverter{

public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
    if(sourceFieldValue==null)
        return null;

    if(sourceFieldValue instanceof List && ((List<?>) sourceFieldValue).size()>0){
       /* This if is an attempt to get the first element of the
        list and return it as a single field, but id doesn't work*/
        Object o = ((List<?>)sourceFieldValue).get(0);
        return o;


    }else{
        /*Updated: using a setter and getter*/
        PersonVo source = (PersonVo) sourceFieldValue;
        List<PersonBo> dest  = new ArrayList<PersonBo>();

        PersonBo bo = new PersonBo();
        bo.setName(source.getName());
        dest.add(bo);
        return dest;
    }
}
}

第3。回答你的问题

要取消单向映射并仅使用CustomConverter,您可以使用以下内容:

推土机XML:

<!-- <mapping type="one-way">
    <class-a>beans.ClientBo</class-a>
    <class-b>beans.ClientVo</class-b>
    <field>
        <a>person[0]</a>
        <b>person</b>
    </field>
</mapping> -->
<mapping>
    <class-a>beans.ClientVo</class-a>
    <class-b>beans.ClientBo</class-b>
    <field custom-converter="converter.CustomObjectToList">
        <a>person</a>
        <b>person</b>
    </field>
</mapping>

CustomConverter:

public class CustomObjectToList implements CustomConverter{

public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
        if(sourceFieldValue==null)
            return null;

        if(sourceFieldValue instanceof PersonVo){
            PersonVo source = (PersonVo) sourceFieldValue;
            List<PersonBo> dest  = new ArrayList<PersonBo>();

            PersonBo bo = new PersonBo();
            bo.setName(source.getName());
            dest.add(bo);
            return dest;

        }else if (sourceFieldValue instanceof List<?>){
            List<PersonBo> source = (List<PersonBo>) sourceFieldValue;

            PersonVo dest = new PersonVo();
            dest.setName(source.get(0).getName());
            return dest;
        }
        else {
            throw new MappingException("Converter TestCustomConverter "
                + "used incorrectly. Arguments passed in were:"
                + existingDestinationFieldValue + " and " + sourceFieldValue);
          }
    }
}