상품 도메인 개발
상품 엔티티에 비즈니스 로직 추가
@Entity
@Getter @Setter
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "dtype")
public abstract class Item {
    @Id @GeneratedValue
    @Column(name = "item_id")
    private Long id;
    private String name;
    private int price;
    private int stockQuantity;
    @ManyToMany(mappedBy = "items")
    private List<Category> categories = new ArrayList<>();
    /**
     * 비즈니스 로직
     */
    
    // stock 증가
    public void addStock(int quantity) {
        this.stockQuantity += quantity;
    }
    // stock 감소
    public void removeStock(int quantity) {
        int restStock = this.stockQuantity - quantity;
        if (restStock < 0) {
            throw new NotEnoughStockException("need more stock");
        }
        this.stockQuantity = restStock;
    }
}public class NotEnoughStockException extends RuntimeException {
    public NotEnoughStockException() {
        super();
    }
    public NotEnoughStockException(String message) {
        super(message);
    }
    public NotEnoughStockException(String message, Throwable cause) {
        super(message, cause);
    }
    public NotEnoughStockException(Throwable cause) {
        super(cause);
    }
}레포지토리
@Repository
@RequiredArgsConstructor
public class ItemRepository {
    private final EntityManager em;
    /**
     * 상품 저장
     * id가 없으면 신규로 보고 persist()를 실행
     * id가 있으면 이미 DB에 저장된 엔티티를 수정한다고 보고 merge()를 실행
     */
    public void save(Item item) {
        if (item.getId() == null) {
            em.persist(item);
        } else {
            em.merge(item);
        }
    }
    /**
     * 단건 조회
     */
    public Item findOne(Long id) {
        return em.find(Item.class, id);
    }
    /**
     * 전체 조회
     */
    public List<Item> findAll() {
        return em.createQuery("select i from Item i", Item.class)
                .getResultList();
    }
}서비스
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ItemService {
    private final ItemRepository itemRepository;
    @Transactional
    public void saveItem(Item item) {
        itemRepository.save(item);
    }
    public List<Item> findItems() {
        return itemRepository.findAll();
    }
    public Item findOne(Long itemId) {
        return itemRepository.findOne(itemId);
    }
}레포지토리에 단순히 위임만 한다.
Last updated