dto가 만들어질때 ArticleForm 클래스에 setter()와 파라미터가 전부 들어있는 생성자가 선언되여져 있어야 하는 이유가 ArticleForm객체가 만들어질때 둘다 사용되기 때문인가요? ps: setter()와 생성자의 차이는 이해했습니다. package com.example.article.dto; import com.example.article.entity.Article; public class ArticleForm { private Long id; private String title; private String content; private Integer cnt; public Integer getCnt() { return cnt; } public void setCnt(Integer cnt) { this.cnt = cnt; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public ArticleForm(String title, String content, Integer cnt) { this.title = title; this.content = content; this.cnt = 0; } public Article toEntity() { return Article.builder() .id(id) .content(content) .title(title) .cnt(cnt) .build(); } @Override public String toString() { return "ArticleForm{" + "id=" + id + ", title='" + title + '\'' + ", content='" + content + '\'' + ", cnt=" + cnt + '}'; } } <form class="container" method="post" action="/articles"> <div class="form-group row"> <label class="col-sm-2 col-form-label">제목</label> <div class="col-sm-10"> <input type="text" name="title" placeholder="제목을 입력하세요" class="form-control"> </div> </div> <div class="form-group row"> <label class="col-sm-2 col-form-label">내용</label> <div class="col-sm-10"> <input type="text" name="content" placeholder="내용을 입력하세요" class="form-control"> </div> </div> <button type="submit" class="btn btn-primary">제출</button> </form> </div>
처음 new ArtileForm()이 만들어질때는 생성자를 통해서 객체가 만들어지고 수정될때는 기존 객체의 값이 setter()를 통해서 값이 변경되는 것 입니다. 즉 create할때 만들어진 dto객체가 update한다고 해서 새로운 dto객체가 만들어지는 것이 아니고 기존에 있던 dto객체에 setter()메서드가 호출되어 값이 바뀌는 것입니다. 그러므로 update가 되어도 dto객체는 하나만 존재하게 되고 객체의 값의 setter()를 통해 바뀌는 것입니다. entity객체도 마찬가지로 create될때 만들어진 객체가 update될때 setter()가 호출되어 객체의 값이 바뀌는 것입니다.