728x90
반응형
package com.example.model.entity.item;
import com.example.model.entity.Category;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@Setter
@ToString(exclude = {"categories"})
@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<Category>();
}
package com.example.model.entity.item;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@Getter
@Setter
@ToString
@DiscriminatorValue("A")
public class Album extends Item {
private String artist;
private String etc;
}
package com.example.model.entity.item;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.Entity;
@Entity
@ToString
@Getter
@Setter
public class Book extends Item {
private String author;
private String isbn;
}
package com.example.model.entity.item;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.Entity;
@Setter
@Getter
@ToString
@Entity
public class Move extends Item {
private String director;
private String actor;
}
package com.example.model.entity;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.MappedSuperclass;
import java.util.Date;
@MappedSuperclass
@Getter
@Setter
public class BaseEntity {
private Date createdDate;
private Date lastModifiedDate;
}
package com.example.model.entity;
import com.example.model.entity.item.Item;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Getter
@Setter
@ToString(exclude = {"items","parent","child"})
@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) {
this.items.add(item);
}
}
package com.example.model.entity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.*;
@Entity
@Setter
@Getter
@ToString(exclude = {"order"})
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(배송)]
}
package com.example.model.entity;
public enum DeliveryStatus {
READY,
COMP
}
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 extends BaseEntity {
@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>();
}
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","delivery"})
@Entity
@Table(name="ORDERS")
public class Order extends BaseEntity {
@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>();
@OneToOne
@JoinColumn(name = "DELIVERY_ID")
private Delivery delivery; //배송정보
@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);
}
public void setDelivery(Delivery delivery) {
this.delivery = delivery;
delivery.setOrder(this);
}
}
package com.example.model.entity;
import com.example.model.entity.item.Item;
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; //주문수량
}
package com.example.model.entity;
public enum OrderStatus {
ORDER,CANCEL
}
728x90
반응형
'프로그램 > 스프링' 카테고리의 다른 글
java.security.NoSuchProviderException: no such provider: BC 에러 발생시 (0) | 2021.07.31 |
---|---|
모델5 @Embeddable (0) | 2021.06.22 |
모델3 연관관계 (0) | 2021.06.22 |
모델2 -연관관계 (0) | 2021.06.22 |
모델1-기본테이블 생성 (0) | 2021.06.22 |
댓글