我正在尝试将Java DTO对象映射到现有的JPA实体对象,而不必执行以下操作:
public MyEntity mapToMyEntity(SomeDTO dto, MyEntity entity) {
entity.setField1(dto.getField1());
entity.setField2(dto.getField2());
...
entity.setField20(dto.getField20());
return entity;
}
到目前为止,我一直在使用ModelMapper,如此:MyEntity entity = modelMapper.map(dto, SomeDTO.class);
,但我正在尝试做的是映射到现有的实体对象,而不是从DTO创建新实体对象。我查看了ModelMapper手册,但没有创建新对象就找不到如何映射。我是否为我可能拥有的每个实体对象手动添加每个成员变量?
答案 0 :(得分:2)
您可以使用dozer mapper或gson。
DozerMapper ex:
Mapper mapper = DozerBeanMapperBuilder.createDefault();
DestinationObject destObject = mapper.map(sourceObject,DestinationClassName.class);
您可以查看github page了解详情
答案 1 :(得分:1)
您可以定义下一个类:
public class ObjectMapperUtils {
private static ModelMapper modelMapper = new ModelMapper();
/**
* Model mapper property setting are specified in the following block.
* Default property matching strategy is set to Strict see {@link MatchingStrategies}
* Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
*/
static {
modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}
/**
* Hide from public usage.
*/
private ObjectMapperUtils() {
}
/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param <D> type of result object.
* @param <T> type of source object to map from.
* @param entity entity that needs to be mapped.
* @param outClass class of result object.
* @return new object of <code>outClass</code> type.
*/
public static <D, T> D map(final T entity, Class<D> outClass) {
return modelMapper.map(entity, outClass);
}
/**
* <p>Note: outClass object must have default constructor with no arguments</p>
*
* @param entityList list of entities that needs to be mapped
* @param outCLass class of result list element
* @param <D> type of objects in result list
* @param <T> type of entity in <code>entityList</code>
* @return list of mapped object with <code><D></code> type.
*/
public static <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
return entityList.stream()
.map(entity -> map(entity, outCLass))
.collect(Collectors.toList());
}
/**
* Maps {@code source} to {@code destination}.
*
* @param source object to map from
* @param destination object to map to
*/
public static <S, D> D map(final S source, D destination) {
modelMapper.map(source, destination);
return destination;
}
}
并根据您的需要使用它:
MyEntity entity = ObjectMapperUtils.map(dto, existingEntity);
答案 2 :(得分:1)
当前ModelMapper
还支持映射到现有对象。
可以执行以下操作(对于伪Spring示例):
MyEntity mye = repository.finById(id);
ModelMapper mm = new ModelMapper();
mm.map(myDTO, mye);
repository.save(mye):
我正在使用2.3.1版,但较早的版本可能也支持此功能
答案 3 :(得分:1)
我们可以在这里使用 Map Struct。 有关如何使用它的参考链接:https://www.baeldung.com/mapstruct
答案 4 :(得分:0)
翻译自葡萄牙语
https://pt.stackoverflow.com/questions/166438/dto-assembler-como-utiliza-lo-realmente
使用模式汇编程序: 您可以通过汇编程序模式将实体转换为DTO,但这是错误的(我认为),它打破了DTO的意义,这是传输对象的标准。看到它可以由几个实体组成。正确的做法是在服务类中使用对象实例的set方法,使用DTO并将它们组装为实体,这只是因为你正在使用标准。
但它有一种方法,即使它是错误的,它也会起作用,但实体x DTO之间只有1 x 1的关联,使用Guava函数。
例如:将Translate转换为使用
转换为对象和列表import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Function;
/**
* Classe de transformação para popular os dados em DTOs, seguindo o pattern
* Transfer Object Assembler.
*/
public class Transformer {
/**
* Executa a transformação de um objeto para um DTO.
*
* @param from
* @param function
* @return <F, T> T
*/
public static <F, T> T transform(F from, Function<? super F, ? extends T> function) {
return (from == null) ? null : function.apply(from);
}
/**
* Executa a transformação de uma lista de objetos para uma lista de DTOs.
*
* @param fromList
* @param function
* @return <F, T> List<T>
*/
public static <F, T> List<T> transform(List<F> source, Function<? super F, ? extends T> function) {
List<T> out = new ArrayList<>(source.size());
for (F from : source) {
out.add(function.apply(from));
}
return out;
}
}
模式汇编程序:
import java.util.List;
import br.com.myapp.model.dto.AuthUserDTO;
import br.com.myapp.model.entity.AuthUser;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
/**
* Classe que transforma entidade USUARIOS em DTO.
*
* @author Dilnei Cunha
*/
public class AuthUserDTOAssembler implements Function<AuthUser, AuthUserDTO>{
/**
* Método responsável por fazer a conversão da entidade USUARIOS em um AuthUserDTO.
*/
@Override
public AuthUserDTO apply(AuthUser e) {
AuthGroupDTO groupDTO = Transformer.transform(e.getAuthGroup(), new AuthGroupDTOAssembler());
return new AuthUserDTO(e.getId(),
e.getName(),
e.getPassword(),
e.getEmail(),
e.getCreationDate(),
e.getLastModificationdate(),
e.getLastAccessDate(),
e.getAtivo(),
e.getUserName(),
e.getRamal(),
groupDTO);
}
}
使用这些模式的服务是什么......
/**
* Método responsável por buscar um AuthUserDTO pelo ID do usuário.
*/
@Override
public AuthUserDTO findById(Long id) {
return Transformer.transform(authUserRepository.findUserById(id), new AuthUserDTOAssembler());
}
现在让我们进行将一个或一个DTO列表转换为对象的逆过程,但请记住该关联是1x1。要做到这一点,只需在函数的实现中反转对象,例如:
import java.util.List;
import br.com.myapp.model.dto.AuthUserDTO;
import br.com.myapp.model.entity.AuthUser;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
/**
* @author Dilnei Cunha
*/
public class AuthUserAssembler implements Function<AuthUserDTO, AuthUser>{
@Override
public AuthUser apply(AuthUserDTO e) {
AuthGroup group = Transformer.transform(e.getAuthGroupDTO(), new AuthGroupAssembler());
return new AuthUser(e.getId(),
e.getName(),
e.getPassword(),
e.getEmail(),
e.getCreationDate(),
e.getLastModificationdate(),
e.getLastAccessDate(),
e.getAtivo(),
e.getUserName(),
e.getRamal(),
group);
}
}