ARTICLE AD BOX
I am trying to update entity fields dynamically using reflection in a Spring Boot application.
I wrote the following code:
public EmployeeDTO updateEmployeeById(Long employeeId, Map<String, Object> updates) { boolean isExist = isExistByEmployeeId(employeeId); if(!isExist) return null; EmployeeEntity employeeEntity = employeeRepository.findById(employeeId).get(); updates.forEach((field, value) -> { Field fieldToBeUpdated = ReflectionUtils.findRequiredField(EmployeeEntity.class, field); fieldToBeUpdated.setAccessible(true); ReflectionUtils.setField(fieldToBeUpdated, employeeEntity, value); }); return modelMapper.map(employeeRepository.save(employeeEntity), EmployeeDTO.class); }But IntelliJ shows this error: Cannot resolve method 'findRequiredField' in 'ReflectionUtils'
I have imported:
import org.springframework.util.ReflectionUtils;Is this a version issue or am I using the wrong method? What is the correct way to get a required field using ReflectionUtils? Console
