I wonder if my reasoning is correct regarding the use of a DTO
Following this logic, am I correct in using a DTO?
@Controller
@RequestMapping("/")
public class CadastroController
{
private final CadastroRepository cadastroRepository;
@Autowired
public CadastroController(CadastroRepository cadastroRepository)
{
this.cadastroRepository = cadastroRepository;
}
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public void save(final CadastroDTO cadastroDTO)
{
CadastroEntity cadastroEntity = new CadastroEntity();
cadastroEntity.setNome(cadastroDTO.getNome());
cadastroRepository.save(cadastroEntity);
}
}
public class CadastroDTO
{
@Getter
@Setter
private String nome;
}
@Entity
@Table(name = "cadastro")
public class CadastroEntity implements Serializable
{
@Id
@GeneratedValue
@Getter
private Long id;
@Getter
@Setter
private String nome;
}
If yes, should I use the DTO only to store the information without the risk of changing the information in the database as would happen to an entity?