728x90
- 주문(Order) 과 배송(Delivery) (일대일 매핑)
- 카테고리(Category) 와 상품(Item) (다대다 매핑)
- 주문(Order) 과 상품(Item) 은 다대다 관계 -> OrderItm(주문상품) 으로 연결해준다.
회원(Member) 엔티티
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
private String name;
private String city;
private String street;
private String zipcode;
@OneToMany(mappedBy = "member") //주인 : Order 클래스의 member.. 회원:주문 = 1:N
private List<Order> orders = new ArrayList<Order>();
//Getter, Setter
..................
}
주문(Order) 엔티티
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = "ORDERS")
public class Order {
@Id @GeneratedValue
@Column(name = "ORDER_ID")
private Long id;
@ManyToOne
@JoinColumn(name = "MEMBER_ID")
private Member member; //주문 회원
@OneToMany(mappedBy = "order") //주인 : OrderItem 클래스의 order.. 상품:주문 = N:N
private List<OrderItem> orderItems = new ArrayList<OrderItem>();
@OneToOne
@JoinColumn(name = "DELIVERY_ID")
private Delivery delivery; //배송정보
private Date orderDate; //주문시간
@Enumerated(EnumType.STRING)
private OrderStatus status;//주문상태
//==연관관계 메서드==//
public void setMember(Member member) {
//기존 관계 제거
if (this.member != null) {
this.member.getOrders().remove(this);
}
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);
}
//Getter, Setter
..................
@Override
public String toString() {
return "Order{" +
"id=" + id +
", orderDate=" + orderDate +
", status=" + status +
'}';
}
}
OrderItem (주문상품) 엔티티
import javax.persistence.*;
@Entity
@Table(name = "ORDER_ITEM")
public class OrderItem {
@Id @GeneratedValue
@Column(name = "ORDER_ITEM_ID")
private Long id;
@ManyToOne
@JoinColumn(name = "ITEM_ID")
private Item item; //주문 상품
@ManyToOne
@JoinColumn(name = "ORDER_ID")
private Order order; //주문
private int orderPrice; //주문 가격
private int count; //주문 수량
//Getter, Setter
........
@Override
public String toString() {
return "OrderItem{" +
"id=" + id +
", buyPrice=" + orderPrice +
", count=" + count +
'}';
}
}
배송 (Delivery) 엔티티
import javax.persistence.*;
@Entity
public class Delivery {
@Id @GeneratedValue
@Column(name = "DELIVERY_ID")
private Long id;
@OneToOne(mappedBy = "delivery")
private Order order;
private String city;
private String street;
private String zipcode;
@Enumerated(EnumType.STRING)
private DeliveryStatus status; //ENUM [READY(준비), COMP(배송)]
//Getter, Setter
...........
}
배송상태 ( DeliveryStatus)
public enum DeliveryStatus {
READY, //준비
COMP //배송
}
카테고리 (Category) 엔티티
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Category {
@Id @GeneratedValue
@Column(name = "CATEGORY_ID")
private Long id;
private String name;
@ManyToMany
@JoinTable(name = "CATEGORY_ITEM",
joinColumns = @JoinColumn(name = "CATEGORY_ID"),
inverseJoinColumns = @JoinColumn(name = "ITEM_ID"))
private List<Item> items = new ArrayList<Item>();
@ManyToOne
@JoinColumn(name = "PARENT_ID")
private Category parent;
@OneToMany(mappedBy = "parent")
private List<Category> child = new ArrayList<Category>();
//==연관관계 메서드==//
public void addChildCategory(Category child) {
this.child.add(child);
child.setParent(this);
}
public void addItem(Item item) {
items.add(item);
}
//Getter, Setter
...............
@Override
public String toString() {
return "Category{" + "id=" + id + ", name='" + name + '\'' + '}';
}
}
상품( Item ) 엔티티
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public 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<Category>();
//Getter, Setter
..................
@Override
public String toString() {
return "Item{" + "id=" + id +", name='" + name + '\'' +", price=" + price + '}';
}
}
'JPA' 카테고리의 다른 글
JPA시작(10)_연관관계 매핑(N:1 / 1:N 양방향) (2) | 2021.06.08 |
---|---|
JPA시작(9)_연관관계 매핑(N:1, 단방향) / 연관관계 사용(등록, 수정, 삭제, 조회) (0) | 2021.06.08 |
JPA시작(8)_객체 연관관계 / 테이블 연관관계 (0) | 2021.06.08 |
JPA시작(7)_필드와 컬럼 매핑_속성에 따른 DDL 생성확인 @Column, @Enumerated, @Temporal, @Lob, @Transient, @Access (0) | 2021.06.07 |
JPA시작(6)_기본키 매핑 (직접할당, 자동생성) (0) | 2021.06.07 |