인프런 커뮤니티 질문&답변
작성자 없음
작성자 정보가 삭제된 글입니다.
DTO관련 질문드립니다.
작성
·
249
0
// Controller
@PostMapping("items/{itemId}/edit")
public String updateItem(@PathVariable("itemId") Long itemId, @ModelAttribute("form") BookForm form) {
itemService.updateItem(new UpdateItemDto(form));
return "redirect:/items";
}
// Entity
@Entity
@DiscriminatorValue("B")
@Getter @Setter
public class Book extends Item {
private String author;
private String isbn;
public void updateBook(UpdateItemDto updateItemDto) {
this.author = updateItemDto.getAuthor();
this.isbn = updateItemDto.getIsbn();
this.setId(updateItemDto.getId());
this.setName(updateItemDto.getName());
this.setPrice(updateItemDto.getPrice());
this.setStockQuantity(updateItemDto.getStockQuantity());
}
}
// Service
@Transactional
public void updateItem(UpdateItemDto updateItemDto) {
Book foundedItem = (Book) itemRepository.findOne(updateItemDto.getId());
foundedItem.updateBook(updateItemDto);
}
선생님 양질의 강의 잘보고있습니다.
엔티티에서 북이 아이템을 상속하고있는데
그 상속하는 필드에 값을 변경시 위의 코드와 같이 셋터를 불러서 값을 변경시켜주면될까요? 아니면
아이템에도 별도의 메소드를 생성하여 값을 변경해주는게 좋은가요?
퀴즈
61%나 틀려요. 한번 도전해보세요!
회원 가입 시 화면 입력 데이터를 엔티티 객체 대신 별도의 Form 객체로 받는 주된 이유는 무엇일까요?
데이터베이스 성능을 최적화하기 위해서
화면 종속적인 데이터나 유효성 검증 로직을 분리하기 위해서
JPA 영속성 컨텍스트와 직접적인 관계를 맺기 위해서
코드의 자동 생성 기능을 활용하기 위해서
답변 1
0





