My simple spring boot hibernate mapping problem

My TaskService

public TaskDTO save(TaskDTO taskEntity){
  Mapper<TaskDTO,TaskEntity> mapper = Mapping.from(taskEntity).to(TaskEntity.class).mapper();
  return taskRepository.save(taskEntity);
}

Hi everyone I am writing simple project and want TaskDTO convert TaskEntity and send TaskManager layer but I don’t know how to do this ? Thanks

Answer

You can use BeanUtils.copyProperties to convert DTO to entity, or entity to DTO.

import org.springframework.beans.BeanUtils
...
BeanUtils.copyProperties(sourceObject, targetObject); 

If you want to ignore some fields, just add them after the targetObject.

BeanUtils.copyProperties(sourceObj, targetObj, "propertyToIgnoreA", "propertyToIgnoreB", "propertyToIgnoreC");

Leave a Reply

Your email address will not be published. Required fields are marked *