I am trying to use Dozer to convert a domain object into DTO objects. So, I want to convert PersistentList, PersistentBag, ... from my domain object to ArrayList, ... to my DTO objects to avoid a lazy problem.
This is an example of two objects in my domain:
public class User {
private Collection<Role> roles;
...
}
public class Role {
private Collection<User> users;
...
}
My DTO objects are the same, except that the class has DTO types. So, to convert the domain to DTO objects, I use the following Dozer mapping:
<configuration>
<custom-converters>
<converter type=com.app.mapper.converter.BagConverter">
<class-a>org.hibernate.collection.PersistentBag</class-a>
<class-b>java.util.List</class-b>
</converter>
</custom-converters>
</configuration>
<mapping>
<class-a>com.app.domain.User</class-a>
<class-b>com.app.dto.UserDTO</class-b>
</mapping>
<mapping>
<class-a>com.app.domain.Role</class-a>
<class-b>com.app.dto.RoleDTO</class-b>
</mapping>
BagConverter is a Dozer custom converter, which is its code:
public class BagConverter<T> extends DozerConverter<PersistentBag, List>{
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<T>();
} else {
listDest = destination;
}
if (source.wasInitialized()) {
listDest.addAll(source);
}
}
return listDest;
}}
, User, PersistentBag . UserDTO. , , UserDTO ArrayList of Role ArrayList of RoleDTO, .
, , dozer . ? , , dto, Java?
.
Sylvain.