본문 바로가기
프로그램/스프링

모델2 -연관관계

by cbwstar 2021. 6. 22.
728x90
반응형
java
닫기
package com.example.model.entity; public enum OrderStatus { ​​​​ORDER,CANCEL }
java
닫기
package com.example.model.entity; import lombok.Getter; import lombok.Setter; import lombok.ToString; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Getter @Setter @ToString @Entity public class Item { ​​​​@Id ​​​​@GeneratedValue ​​​​@Column(name="ITEM_ID") ​​​​private Long id; ​​​​private String name; //이름 ​​​​private int price; //가격 ​​​​private int stockQuntity; //재고수량 }
java
닫기
package com.example.model.entity; import lombok.Getter; import lombok.Setter; import lombok.ToString; import javax.persistence.*; import java.util.ArrayList; import java.util.List; @Getter @Setter @ToString(exclude = {"orders"}) @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") ​​​​private List<Order> orders = new ArrayList<Order>(); }
java
닫기
package com.example.model.entity; import lombok.Getter; import lombok.Setter; import lombok.ToString; import javax.persistence.*; import java.util.ArrayList; import java.util.Date; import java.util.List; @Getter @Setter @ToString(exclude = {"member","orderItems"}) @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") ​​​​private List<OrderItem> orderItems = new ArrayList<OrderItem>(); ​​​​@Temporal(TemporalType.TIMESTAMP) ​​​​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); ​​​​} }
java
닫기
package com.example.model.entity; import lombok.Getter; import lombok.Setter; import lombok.ToString; import javax.persistence.*; @ToString(exclude = {"item","order"}) @Getter @Setter @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; //주문수량 }

 

728x90
반응형

'프로그램 > 스프링' 카테고리의 다른 글

모델4 공통 상속  (0) 2021.06.22
모델3 연관관계  (0) 2021.06.22
모델1-기본테이블 생성  (0) 2021.06.22
엔티티 생성 및 기본 테스트  (0) 2021.06.22
pom.xml  (0) 2021.06.22

댓글



"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."