Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4기 황준호]JPA : Mission 3 제출입니다. #303

Open
wants to merge 1 commit into
base: juno-junho
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kdt.kdtjpa.domain.order;

import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ Getter 따로 안쓰이고 있는 것 같은데 삭제하면 좋을 것 같습니다!

@MappedSuperclass
public abstract class BaseEntity {
@Column(name = "created_by")
private String createdBy;

@Column(name = "created_at", columnDefinition = "TIMESTAMP")
private LocalDateTime createdAt;
}
34 changes: 34 additions & 0 deletions kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.kdt.kdtjpa.domain.order;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

@Entity
@Table(name = "item")
@Getter
@Setter
@DiscriminatorColumn(name = "DTYPE")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Item extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

private int price;
private int stockQuantity;
Comment on lines +20 to +21
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엔터가 빠져 있습니다!


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_item_id", referencedColumnName = "id")
private OrderItem orderItem;

public void setOrderItem(OrderItem orderItem) {
if (Objects.nonNull(this.orderItem)) {
this.orderItem.getItems().remove(this);
}
this.orderItem = orderItem;
orderItem.getItems().add(this);
}
}
38 changes: 38 additions & 0 deletions kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Member.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.kdt.kdtjpa.domain.order;

import jakarta.persistence.*;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "member")
@Getter
public class Member extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

@Column(name = "name", nullable = false, length = 30)
private String name;

@Column(name = "nick_name", nullable = false, length = 30, unique = true)
private String nickName;

@Column(name = "age", nullable = false)
private int age;

@Column(name = "address", nullable = false)
private String address;

@Column(name = "description")
private String description;

@OneToMany(mappedBy = "member") // 연관관계 주인 설정
private List<Order> orders = new ArrayList<>();

public void addOrder(Order order) { // 연관관계 편의 메서드
order.setMember(this);
}
}
45 changes: 45 additions & 0 deletions kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.kdt.kdtjpa.domain.order;

import jakarta.persistence.*;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "orders")
@Getter
public class Order extends BaseEntity {
@Id
@Column(name = "id")
private String uuid;

@Lob
private String memo;

@Enumerated(value = EnumType.STRING)
private OrderStatus orderStatus;

@Column(name = "order_datetime", columnDefinition = "TIMESTAMP")
private LocalDateTime orderDateTime;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "memeber_id", referencedColumnName = "id")
private Member member;

@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
List<OrderItem> orderItems;

public void setMember(Member member) {
if (Objects.nonNull(this.member)) {
member.getOrders().remove(this);
}
this.member = member;
member.getOrders().add(this);
}

public void addOrderItem(OrderItem orderItem) {
orderItem.setOrder(this);
}
}
39 changes: 39 additions & 0 deletions kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/OrderItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.kdt.kdtjpa.domain.order;

import jakarta.persistence.*;
import lombok.Getter;

import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "order_item")
@Getter
public class OrderItem extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

private int price;
private int quantity;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id", referencedColumnName = "id")
private Order order;

@OneToMany(mappedBy = "orderItem", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Item> items;

public void setOrder(Order order) {
if (Objects.nonNull(this.order)) {
this.order.getOrderItems().remove(this);
}
this.order = order;
order.getOrderItems().add(this);
}

public void addItem(Item item) {
item.setOrderItem(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kdt.kdtjpa.domain.order;

public enum OrderStatus {
OPENED, CANCELLED
}