当我从数据库获取实体并修改它时,我有一个简单的场景。
我不知道哪些字段正在被修改,所以我只是从db获取当前实体并通过新的DTO(newModifiedUserDTO)设置所有值。
代码变得杂乱,丑陋和漫长。
也许你可以建议我(使用java 8的lambda或任何其他技术如何使这段代码更短更有效?
@Transactional
@Override
public void modifyUserEntity(UserEntityDTO newModifiedUserDTO) {
UserEntity oldUserEntity = usersRepository.findByUsername(newModifiedUserDTO.getUsername());
oldUserEntity.getExternalUsersEntity().setFirstName(newModifiedUserDTO.getExternalUsersEntity().getFirstName());
oldUserEntity.getExternalUsersEntity().setLastName(newModifiedUserDTO.getExternalUsersEntity().getLastName());
oldUserEntity.getExternalUsersEntity().setEnteredCompany(newModifiedUserDTO.getExternalUsersEntity().getEnteredCompany());
oldUserEntity.getExternalUsersEntity().setCountry(newModifiedUserDTO.getExternalUsersEntity().getCountry());
oldUserEntity.getExternalUsersEntity().setState(newModifiedUserDTO.getExternalUsersEntity().getState());
oldUserEntity.getExternalUsersEntity().setCity(newModifiedUserDTO.getExternalUsersEntity().getCity());
oldUserEntity.getExternalUsersEntity().setAddress1(newModifiedUserDTO.getExternalUsersEntity().getAddress1());
oldUserEntity.getExternalUsersEntity().setAddress2(newModifiedUserDTO.getExternalUsersEntity().getAddress2());
oldUserEntity.getExternalUsersEntity().setZipCode(newModifiedUserDTO.getExternalUsersEntity().getZipCode());
oldUserEntity.getExternalUsersEntity().setTelephone(newModifiedUserDTO.getExternalUsersEntity().getTelephone());
oldUserEntity.getExternalUsersEntity().setFax(newModifiedUserDTO.getExternalUsersEntity().getFax());
oldUserEntity.getExternalUsersEntity().setShippingCountry(newModifiedUserDTO.getExternalUsersEntity().getShippingCountry());
oldUserEntity.getExternalUsersEntity().setShippingState(newModifiedUserDTO.getExternalUsersEntity().getShippingState());
oldUserEntity.getExternalUsersEntity().setShippingCity(newModifiedUserDTO.getExternalUsersEntity().getShippingCity());
oldUserEntity.getExternalUsersEntity().setShippingZip(newModifiedUserDTO.getExternalUsersEntity().getShippingZip());
oldUserEntity.getExternalUsersEntity().setShippingTelephone(newModifiedUserDTO.getExternalUsersEntity().getShippingTelephone());
oldUserEntity.getExternalUsersEntity().setShippingFax(newModifiedUserDTO.getExternalUsersEntity().getShippingFax());
oldUserEntity.getExternalUsersEntity().setShippingAddress1(newModifiedUserDTO.getExternalUsersEntity().getShippingAddress1());
oldUserEntity.getExternalUsersEntity().setShippingAddress2(newModifiedUserDTO.getExternalUsersEntity().getShippingAddress2());
oldUserEntity.getExternalUsersEntity().setWorkspace(newModifiedUserDTO.getExternalUsersEntity().getWorkspace());
oldUserEntity.setModifyDate(CisUtils.getCurrentDate());
}
根据一些建议我尝试使用Spring utils:
copyProperties(newModifiedUserDTO.getExternalUsersEntity(), modifiedUserEntity.getExternalUsersEntity(), "firstName", "lastName", "enteredCompany",
"country", "state", "address1", "address2", "zipCode","telephone","fax","shippingCountry","shippingCity","shippingZip","shippingTelephone","shippingFax","shippingAddress1",
"shippingAddress2","workspace");
但由于某种原因,更改未反映在数据库中(使用hibernate)
任何想法?
谢谢, 射线。
答案 0 :(得分:0)
检查Apache BeanUtils。它具有copyProperties()
方法,可以将所有值从一个对象复制到另一个对象。 Spring框架中也有类似的方法(相同的类,同名);后者允许指定属性而不是来复制可能有用的属性。