6

ブルドーザーマッピングファイルの設定についてサポートが必要です。

主に、UseruserobejctをLonguserIdに変換する方法を知りたいです。
したがって、map:user >> userId
しかし、comment>>commentIdやaddress>>addressIdなどの複数のオブジェクトがあります

そのため、各フィールドのマッピングを作成するだけでなく、よりエレガントなものが必要です。すべてのオブジェクトは、ロード可能なインターフェイスを実装しています。

以下のコードは、getParameter()DozerConverterメソッドのおかげで機能するようになりましたが、私が書いたコンバーターよりも良い方法を知っている場合は、私に知らせてください。

// dozer.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

<configuration>
<custom-converters>
  <converter type="project.shared.domain.dto.dozer.LoadableIdConverter" >
    <class-a>project.shared.domain.Loadable</class-a>
    <class-b>java.lang.Long</class-b>
  </converter>
</custom-converters>
</configuration>

<mapping>
    <class-a>project.shared.domain.Suggestion</class-a>
    <class-b>project.shared.domain.dto.DTOSuggestion</class-b>
    <field custom-converter-param="User">
        <a>user</a>
        <b>userId</b>
    </field>
</mapping>

</mappings>\

//Springアプリケーションコンテキスト

<bean id="loadableIdConverter" class="project.shared.domain.dto.dozer.LoadableIdConverter">
    <property name="userService" ref="userService"/>
    <property name="commentService" ref="commentService"/>
    <property name="addressService" ref="addressService"/>
</bean>
<bean id="gwtMapper" class="org.dozer.DozerBeanMapper">
    <property name="mappingFiles">
        <list>
            <value>classpath:/dozer.xml</value>
        </list>
    </property>

    <property name="customConverters">
        <list>
            <ref bean="loadableIdConverter"/>
        </list>
    </property>
</bean>

//標準のHibernateオブジェクト

public class Suggestion implements Serializable, Loadable {
    private long id = -1;
    private Date dateCreated;

    private User user; //trying to use dozer to covert this bad boy to Long userId
    //...
} 

//DTOオブジェクト

public class DTOSuggestion implements IsSerializable {
   private long id = -1;
   private Date dateCreated;

   private Long userId; //trying to get this ID via the dozer converter
   //...
}

//ロード可能なインターフェース

public interface Loadable extends Serializable {
    public long getId();
    public void setId(long id);
}

//ドーザーコンバーター

public class LoadableIdConverter extends DozerConverter<Loadable, Long> {

    private UserService userService; //configured in applicationcontext
    private AddressService addressService; //configured in applicationcontext
    private CommentService commentService; //configured in applicationcontext 

    public LoadableIdConverter() {
        super(Loadable.class, Long.class);
    }

    public Long convertTo(Loadable object, Long id) {
        return object.getId();
    }

    public Loadable convertFrom(Long id, Loadable object) {
        if (id < 0) return null;

        String loadable = getParameter();
        if (loadable.equalsIgnoreCase("User"))
            return userService.get(User.class, id);
        if (loadable.equalsIgnoreCase("Address"))
            return addressService.get(Address.class, id);
        if (loadable.equalsIgnoreCase("Comment"))
            return commentService.get(Comment.class, id);
        return null;
    }
}
4

1 に答える 1

4

コンバーターのパラメーターを回避するために使用できるトリックが1つあります。CustomConverterインターフェースを実装しているDozerの古いカスタムコンバーターアプローチにフォールバックすると、existingDestinationValueとdestinationClassの2つの追加パラメーターを取得します。

convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) 

これらの値を使用することで、リフレクションを介して宛先フィールドをイントロスペクトし、Loadableインターフェイスの予想される具体的な実装を知ることができます。これは、もちろん具体的なタイプでフィールドタイプを定義する場合にのみ機能します。しかし、あなたはすでにあなたの例にそれを持っているので、これは問題ではないはずです。CustomConverterの実装は、マッピングの方向を手動で決定する必要があるため、より冗長になりますが、マッピングプロセス中に何が起こっているかを完全に制御できます。

于 2010-12-30T21:02:55.653 に答える