Spring boot JPA 4 - 연관관계 테이블 매핑

UnKnown12·2022년 12월 8일
0

Spring Boot

목록 보기
8/13

📌 개요

상단의 테이블 구조를 JPA - 연관관계 매핑을 통해 설계하고자 한다

📌 MEMBER 테이블

@Entity
@Getter @Setter
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")
	private List<Order> orders = 
		new ArrayList<Order>();
	
	public void addOrder(Order order) {
		order.setMember(this);
		this.orders.add(order);
	}
	
}

📌 ORDERS 테이블

@Entity
@Table(name = "ORDERS")
@Getter @Setter
public class Order {

	@Id @GeneratedValue
	@Column(name = "ORDER_ID")
	private Long id;
//	@Column(name = "MEMBER_ID")
//	private Long memberId;
	private LocalDateTime orderDate;
	private String status;
	
	@ManyToOne
	@JoinColumn(name = "MEMBER_ID")
	private Member member;
	
	@OneToMany(mappedBy = "order")
	private List<OrderItem> orderItems
		 = new ArrayList<OrderItem>();
	
	public void addOrderItem(OrderItem orderItem) {
		orderItem.setOrder(this);
		this.orderItems.add(orderItem);
	}
	
}

📌 ORDER_ITEM 테이블

@Entity
@Getter @Setter
public class OrderItem {

	@Id @GeneratedValue
	@Column(name = "ORDER_ITEM_ID")
	private Long id;
//	@Column(name = "ORDER_ID")
//	private Long orderId;
//	@Column(name = "ITEM_ID")
//	private Long itemId;
	private int orderPrice;
	private int count;
	
	@ManyToOne
	@JoinColumn(name = "ORDER_ID")
	private Order order;
	
	@ManyToOne
	@JoinColumn(name = "ITEM_ID")
	private Item item;
	
}

📌 ITEM 테이블

@Entity
@Getter @Setter
public class Item {

	@Id @GeneratedValue
	@Column(name = "ITEM_ID")
	private Long Id;
	private String name;
	private int price;
	private int stockQuantity;
	
	@OneToMany(mappedBy = "item")
	private List<OrderItem> orderItems
		= new ArrayList<OrderItem>();
	
	public void addOrderItem(OrderItem orderItem) {
		orderItem.setItem(this);
		this.orderItems.add(orderItem);
	}
	
}
profile
Hyobin12

0개의 댓글