title: Dozer mapping for Hibernate object to DTO
body: <p>I try to use Dozer to convert my domain entity to DTO objects.
  So, I want to convert PersistentList, PersistentBag, ... from my domain entity to ArrayList, ... in my DTO objects to avoid lazy problem.</p>
  
  <p>This is an example of two of my domain entity :</p>
  
  <pre><code>public class User {
        private Collection&lt;Role&gt; roles;
        ...
  }
  
  public class Role {
        private Collection&lt;User&gt; users;
        ...
  }
  </code></pre>
  
  <p>My DTO objects are the same except that class are of types DTO. So, to convert domain to DTO objects, I use the following Dozer mapping :</p>
  
  <pre><code> &lt;configuration&gt;
    &lt;custom-converters&gt;
     &lt;converter type=com.app.mapper.converter.BagConverter"&gt;
      &lt;class-a&gt;org.hibernate.collection.PersistentBag&lt;/class-a&gt;
      &lt;class-b&gt;java.util.List&lt;/class-b&gt;
     &lt;/converter&gt;
    &lt;/custom-converters&gt;
   &lt;/configuration&gt;
  
   &lt;mapping&gt;
    &lt;class-a&gt;com.app.domain.User&lt;/class-a&gt;
    &lt;class-b&gt;com.app.dto.UserDTO&lt;/class-b&gt;
   &lt;/mapping&gt;
  
   &lt;mapping&gt;
    &lt;class-a&gt;com.app.domain.Role&lt;/class-a&gt;
    &lt;class-b&gt;com.app.dto.RoleDTO&lt;/class-b&gt;
   &lt;/mapping&gt;
  </code></pre>
  
  <p>BagConverter is a Dozer custom converter and that is its code :</p>
  
  <pre><code>  public class BagConverter&lt;T&gt; extends DozerConverter&lt;PersistentBag, List&gt;{
  
   public BagConverter() {
    super(PersistentBag.class, List.class);
   }
  
   public PersistentBag convertFrom(List source, PersistentBag destination) {
    PersistentBag listDest = null;
  
    if (source != null) {
  
     if (destination == null) {
      listDest = new PersistentBag();
     } else {
      listDest = destination;
     }
  
     listDest.addAll(source);
    }
  
    return listDest;
   }
  
   public List convertTo(PersistentBag source, List destination) {
    List listDest = null;
  
    if (source != null) {
  
     if (destination == null) {
      listDest = new ArrayList&lt;T&gt;();
     } else {
      listDest = destination;
     }
  
     if (source.wasInitialized()) {
      listDest.addAll(source);
     }
    }
  
    return listDest;
   }}
  </code></pre>
  
  <p>So, I get a User object that contains a PersistentBag with roles. I apply dozer mapper map on that object to obtain UserDTO object. The result that I obtain is a UserDTO object with a ArrayList of Role and no an ArrayList of RoleDTO like I wished.</p>
  
  <p>I thought that even if I used custom converter, dozer will convert the content of my list. It's not the right way ? If no, how to do to convert my domain entity to dto object by replacing persitent collections to classic java collections ?</p>
  
  <p>Thanks for your help.</p>
  
  <p>Sylvain.</p>