JPAデザイン会員を利用したオーダーサービス2


会員(メンバー)リポジトリ設計

@Repository						//ComponentScan이 가능하도록 하는 Repository어노테이션
@RequiredArgsConstructor		// final이 붙은 필드로 생성자를 만들어줌, 
								// 의존관계 주입 로직을 누락하는 실수를 방지할 수 있다. 
                                // 생성자를 한개만 두고 @Autowired를 생략할 수 있음
public class MemberJpaRepository {

    @Autowired
    private final EntityManager em;	//영속성 컨텍스트 선언

    public void save(Member member){
        em.persist(member);			//영속성 컨텍스트에 insert 주입
    }	

    public Member findOne(Long id){
        return em.find(Member.class, id);	//영속성 컨텍스트 내 select를 통한  단건 조회
    }

    public List<Member> findAll(){
        return em.createQuery("select m from Member m", Member.class).getResultList();	
        //JPQL이용한 조회
    }

    public List<Member> findByName(String name){
        return em.createQuery("select m from Member m where m.name = :name", Member.class)
                .setParameter("name",name)
                .getResultList();
      //JPQL을 이용하여 동적으로 데이터가 바인딩 되기 위한 setParameter , :name
    }

}
詳細については、JPQL:https://victorydntmd.tistory.com/205を参照してください.

会員サービスデザイン

package hello.hellospring.service;

import hello.hellospring.domain.Member;
import hello.hellospring.repository.MemberJpaRepository;
import hello.hellospring.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)			
@RequiredArgsConstructor    //final 변수를 가진 필드의 생성자를 명시
public class MemberJpaService {

    private final MemberJpaRepository memberJpaRepository;

    
    /*
    * 회원 가입
    * */
    @Transactional
    public Long join(Member member){

        validateDuplicateMember(member);        //중복 회원 검증
        memberJpaRepository.save(member);
        return member.getId();
    }
    
    //회원 전체 조회
    private void validateDuplicateMember(Member member) {
        List<Member> findMembers = memberJpaRepository.findByName(member.getName());
        if(!findMembers.isEmpty()){
            throw new IllegalStateException("이미 존재하는 회원입니다.");
        }
    }

    //회원 전체 조회

    public List<Member> findMembers(){
        return memberJpaRepository.findAll();
    }

    public Member findOne(Long memberId){
        return memberJpaRepository.findOne(memberId);
    }
}

ドメインモデルモードのエンティティ上でのサービスロジックの実装


商品実体設計

package hello.hellospring.domain;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.aspectj.weaver.ast.Or;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static javax.persistence.FetchType.*;

@Entity
@Table(name = "orders")
@Getter @Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Order {

    @Id
    @GeneratedValue
    @Column(name = "order_id")
    private Long id;

    @ManyToOne(fetch = LAZY)  //ManyToOne 또는 OnetoOne 같은 한개로 가는 것은 FetchType.LAZY로 설정해줘야함(실무에서)
    @JoinColumn(name = "member_id")
    private Member member;

    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
    private List<OrderItem> orderItems = new ArrayList<>();

    @OneToOne(fetch = LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name= "delivery_id")
    private Delivery delivery;

    private LocalDateTime orderDate;  //주문시간

    @Enumerated(EnumType.STRING)
    private OrderStatus status; // 주문상태 [ORDER, CANCEL]

    //==연관관계 메서드==//
    public void setMember(Member member){
        this.member = member;
        member.getOrders().add(this);
    }

    public void addOrderItem(OrderItem orderItem){
        orderItems.add(orderItem);
        orderItem.setOrder(this);
    }

    public void setDelivery(Delivery delivery){
        this.delivery = delivery;
        delivery.setOrder(this);
    }

    //=생성 메서드=//
    public static Order createOrder(Member member, Delivery delivery, OrderItem... orderItems){
        Order order = new Order();
        order.setMember(member);
        order.setDelivery(delivery);
        for(OrderItem orderItem : orderItems){
            order.addOrderItem(orderItem);
        }
        order.setStatus(OrderStatus.ORDER);
        order.setOrderDate(LocalDateTime.now());
        return order;
    }

    //==비즈니스 로직==//

    /*
    * 주문 취소
    */
    public void cancel(){
        if(delivery.getStatus() == DeliveryStatus.COMP){
            throw new IllegalStateException("이미 배송완료된 상품은 취소가 불가능합니다.");
        }

        this.setStatus(OrderStatus.CANCEL);
        for(OrderItem orderItem : orderItems){
            orderItem.cancel();
        }
    }


    //==조회 로직==//
    /*
    * 전체 주문 가격 조회
    * */
    public int getTotalPrice(){

        /*
        int totalPrice = 0;

        for(OrderItem orderItem : orderItems){
            totalPrice += orderItem.getTotalPrice();
        }
        아래 스트림 표현과 동일 */

        int totalPrice = orderItems.stream().mapToInt(OrderItem::getTotalPrice).sum();

        return totalPrice;
    }



}
@NoArgsConstructor(access = AccessLevel.PROTECTED) :
パラメータのないデフォルトの作成者を保護し、ビジネスロジックで作成者を作成し、Setterを使用します.
入力データのブロック
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL) :