4

カスタム コンバーターを使用した 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 は呼び出されません。

問題は、両方のタイプが文字列であることだと思いますが、どうすればこれを機能させることができますか?

回避策として、一方向マッピングを 2 つ作成しました。これは標準的な解決策ですか、それとも動作はバグですか?

4

3 に答える 3

2

はい、問題は、ソース クラスと宛先クラスが同じであることです。のドーザーソースは次の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() + ")!");
    }

  }

convertFromおよびconvertToメソッド (新しい API の一部) を使用する代わりに、チュートリアルCustomConverter.convertに示されているように実装する必要がある元の方法で実行します。

于 2011-02-18T14:59:22.133 に答える
0

数年後に同じ種類の問題が発生しましたが、どういうわけか新しい API である DozerConverter API がまだ双方向として正しく機能しません !!

したがって、ここでアドバイスされているこれらすべての複雑なソリューションに入るのではなく、この問題を克服するために 2 つの一方向マッピングも作成しました (with ) 。そして、私の変換が機能し始めました。以下のような DozerConverter API を使用しています。

public class MapToStringConverter は DozerConverter を拡張します

于 2016-03-28T18:26:01.350 に答える