• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

DTO관련 질문드립니다.

21.06.13 11:03 작성 조회수 139

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);
    }

선생님 양질의 강의 잘보고있습니다.

엔티티에서 북이 아이템을 상속하고있는데

그 상속하는 필드에 값을 변경시 위의 코드와 같이 셋터를 불러서 값을 변경시켜주면될까요? 아니면

아이템에도 별도의 메소드를 생성하여 값을 변경해주는게 좋은가요?

답변 1

답변을 작성해보세요.

0

안녕하세요. const.takeo님

setter를 직접 사용해도 되지만, 별도의 의미있는 메서드를 만드는 것이 조금 더 나은 선택이라 생각합니다.

감사합니다.