java - 带有自定义转换器的推土机双向映射(字符串,字符串)不可能吗?

标签 java mapping dozer

我有一个带有自定义转换器的 Dozer 映射:

<mapping>
    <class-a>com.xyz.Customer</class-a>
    <class-b>com.xyz.CustomerDAO</class-b>
    <field custom-converter="com.xyz.DozerEmptyString2NullConverter">
        <a>customerName</a>
        <b>customerName</b>
    </field>
</mapping>

和转换器:

public class DozerEmptyString2NullConverter extends DozerConverter<String, String> {

    public DozerEmptyString2NullConverter() {
        super(String.class, String.class);
    }

    public String convertFrom(String source, String destination) {
        String ret = null;
        if (source != null) {
            if (!source.equals(""))
            {
                ret = StringFormatter.wildcard(source);
            } 
        }
        return ret;
    }

    public String convertTo(String source, String destination) {
        return source;
    }
}

当我在一个方向(Customer -> CustomerDAO)调用映射器时,方法“convertTo”被调用。

由于 Dozer 能够处理双向映射,我希望一旦我在相反方向调用映射器,就会调用方法“convertFrom”。

但是永远不会调用 convertTo 方法。

我怀疑问题是,这两种类型都是字符串 - 但我怎样才能让它工作?

作为一种解决方法,我创建了两个单向映射,这是标准解决方案,还是该行为是一个错误?

最佳答案

是的,问题是你的源类和目标类是一样的。这是推土机 source对于 DozerConverter:

  public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
    Class<?> wrappedDestinationClass = ClassUtils.primitiveToWrapper(destinationClass);
    Class<?> wrappedSourceClass = ClassUtils.primitiveToWrapper(sourceClass);

    if (prototypeA.equals(wrappedDestinationClass)) {
      return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue);
    } else if (prototypeB.equals(wrappedDestinationClass)) {
      return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue);
    } else if (prototypeA.equals(wrappedSourceClass)) {
      return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue);
    } else if (prototypeB.equals(wrappedSourceClass)) {
      return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue);
    } else if (prototypeA.isAssignableFrom(wrappedDestinationClass)) {
      return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue);
    } else if (prototypeB.isAssignableFrom(wrappedDestinationClass)) {
      return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue);
    } else if (prototypeA.isAssignableFrom(wrappedSourceClass)) {
      return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue);
    } else if (prototypeB.isAssignableFrom(wrappedSourceClass)) {
      return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue);
    } else {
      throw new MappingException("Destination Type (" + wrappedDestinationClass.getName()
          + ") is not accepted by this Custom Converter (" 
          + this.getClass().getName() + ")!");
    }

  }

不是使用 convertFromconvertTo 方法(它们是新 API 的一部分),而是按照原来的方式执行,您必须实现 CustomConverter。转换tutorial所示.

关于java - 带有自定义转换器的推土机双向映射(字符串,字符串)不可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5041832/

相关文章:

java - 小程序签名 - 丢失 keystore 密码

Java Spring 友好的 URL 映射问题

java - 如何使用自定义映射器映射字段并将字段名称映射保存在 MapperFacade 或 MapperFactory 中?

c# - 如何在每个层次结构 (TPH) 映射的表中共享公共(public)列名

java - Dozer 映射到现有对象非空字段

java - 按字符串数组字段过滤 Realm 结果

java - 将 for 循环结果保存为字符串

GWT + 实体 + JPA + DTO + 推土机

java - JNI eclipse Javah 自动生成

java - 将列表映射到 Dozer 中的列表