Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

[#23] create Entity, schema about Item & Order #24

Open
wants to merge 2 commits into
base: feature/19
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
3 changes: 3 additions & 0 deletions src/main/java/me/jjeda/mall/accounts/domain/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.common.model.Address;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
Expand All @@ -27,6 +29,7 @@ public class Account {

@Id
@GeneratedValue
@Column(name = "account_id")
private Long id;

private String nickname;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/jjeda/mall/accounts/dto/AccountDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.domain.AccountRole;
import me.jjeda.mall.accounts.domain.AccountStatus;
import me.jjeda.mall.accounts.domain.Address;
import me.jjeda.mall.common.model.Address;

import javax.persistence.Column;
import javax.validation.constraints.Email;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package me.jjeda.mall.accounts.domain;
package me.jjeda.mall.common.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.Embeddable;

@Embeddable
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class Address {

private String city;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/me/jjeda/mall/items/domain/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.jjeda.mall.items.domain;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
@Getter @Setter
public class Category {

@Id @GeneratedValue
@Column(name = "category_id")
private Long id;

private String name;
}
31 changes: 31 additions & 0 deletions src/main/java/me/jjeda/mall/items/domain/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.jjeda.mall.items.domain;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;

@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<ItemCategory> itemCategories = new ArrayList<>();
}
29 changes: 29 additions & 0 deletions src/main/java/me/jjeda/mall/items/domain/ItemCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.jjeda.mall.items.domain;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
@Getter @Setter
public class ItemCategory {

@Id @GeneratedValue
@Column(name = "item_category_id")
private Long id;

@ManyToOne
@JoinColumn(name = "item_id")
private Item item;

@ManyToOne
@JoinColumn(name = "category_id")
private Category category;

}
34 changes: 34 additions & 0 deletions src/main/java/me/jjeda/mall/orders/domain/Delivery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.jjeda.mall.orders.domain;

import lombok.Getter;
import lombok.Setter;
import me.jjeda.mall.common.model.Address;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
@Getter @Setter
public class Delivery {

@Id
@GeneratedValue
@Column(name = "delivery_id")
private Long id;

@OneToOne(mappedBy = "delivery")
private Order order;

@Embedded
private Address address;

@Enumerated(EnumType.STRING)
private DeliveryStatus status;

}
10 changes: 10 additions & 0 deletions src/main/java/me/jjeda/mall/orders/domain/DeliveryStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.jjeda.mall.orders.domain;

/**
* READY : 배송 준비
* DELIVERY : 배송 중
* COMP : 배송 완료
*/
public enum DeliveryStatus {
Copy link
Member

Choose a reason for hiding this comment

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

👍

READY, DELIVERY, COMP
}
52 changes: 52 additions & 0 deletions src/main/java/me/jjeda/mall/orders/domain/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.jjeda.mall.orders.domain;

import lombok.Getter;
import lombok.Setter;
import me.jjeda.mall.accounts.domain.Account;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter @Setter
@Table(name = "orders", indexes = {@Index(columnList = "orderAt")} )
public class Order {
Copy link
Member

Choose a reason for hiding this comment

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

아직 안만드신것이겠지만 결제정보도 있으면 좋을 것 같네요~


@Id @GeneratedValue
@Column(name = "order_id")
private Long id;

@ManyToOne
Copy link
Member

Choose a reason for hiding this comment

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

ManyToOne인 이유를 알 수 있을까요?

Copy link
Collaborator Author

@jjeda jjeda Sep 29, 2019

Choose a reason for hiding this comment

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

우선 한 구매자가 여러개의 주문을 하는 상황이 있기 때문에 Account : Order 는 일대다 매핑으로 설계 하였습니다. Account 에서 @OneToMany로 설정하지 않고 Order 에서 @ManyToOne 으로 설계한 이유는

  • One 이 연관관계의 주인이라면 One 에서 수정했을 때 Many Table에 쿼리가 날아가는 상황이 발생하고
  • 이는 직관적이지 못하기 때문에 Many 를 관계의 주인으로 설정하고 (@ManyToOne 단방향)
  • 만약 추후에 Account 에서 Order 에 참조해야 한다면 양방향 매핑

위와 같은 이유 입니다!

@JoinColumn(name = "account_id")
private Account account;

/**
* 주로 access 하는 Order를 연관관계의 주인으로 설정
*/
@OneToOne
@JoinColumn(name = "delivery_id")
private Delivery delivery;

@OneToMany(mappedBy = "order")
@NotNull
private List<OrderItem> orderItems;

private LocalDateTime orderAt;

@Enumerated(EnumType.STRING)
private OrderStatus status;
}
33 changes: 33 additions & 0 deletions src/main/java/me/jjeda/mall/orders/domain/OrderItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.jjeda.mall.orders.domain;

import lombok.Getter;
import lombok.Setter;
import me.jjeda.mall.items.domain.Item;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
@Getter @Setter
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")
Copy link
Member

Choose a reason for hiding this comment

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

보통 Order에서 OrderItem을 join해서 가져올텐데 여기에 명시해주지 않아도 되지 않을까요?
주문메뉴로 주문데이터를 불러올 가능성이 있는지도 궁금합니다

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

이 부분도 위에와 같은 이유입니다!

를 참고 하였습니다!

private Order order;

private int orderPrice;

private int count;
}
9 changes: 9 additions & 0 deletions src/main/java/me/jjeda/mall/orders/domain/OrderStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package me.jjeda.mall.orders.domain;

/**
* ORDER : 주문
* CANCEL : 주문취소
*/
public enum OrderStatus {
ORDER, CANCEL
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
spring.datasource.url=jdbc:mysql://localhost:3306/mall
spring.datasource.url=jdbc:mysql://localhost:3306/mall?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root


spring.jpa.hibernate.ddl-auto=validate

spring.jpa.show-sql=true
Expand Down
62 changes: 59 additions & 3 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
create table account (
id bigint not null,
account_id bigint not null,
city varchar(255),
street varchar(255),
zip_code varchar(255),
Expand All @@ -10,8 +10,64 @@ create table account (
password varchar(255),
phone varchar(255),
status varchar(255),
primary key (id));
primary key (account_id));

create table account_account_role (
account_id bigint not null,
account_role varchar(255));
account_role varchar(255),
foreign key (account_account_id) references account (account_id));

create table category (
category_id bigint not null,
name varchar(255),
primary key (category_id));

create table item (
item_id bigint not null,
name varchar(255),
price integer not null,
stock_quantity integer not null,
primary key (item_id));

create table item_category (
item_category_id bigint not null,
category_id bigint,
Copy link
Member

Choose a reason for hiding this comment

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

들여쓰기가 조금씩 차이가 있는데 이를 맞춰주는것이 컨벤션상 좋을 것 같습니다~

item_id bigint,
primary key (item_category_id),
foreign key (category_id) references category (category_id),
foreign key (item_id) references item (item_id));
Copy link
Member

Choose a reason for hiding this comment

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

);는 개행해주면 가독성면에서 더 좋을 것 같습니다~


create table orders (
order_id bigint not null,
order_at datetime not null,
status varchar(255),
account_id bigint,
delivery_id bigint,
index (order_at),
primary key (order_id),
foreign key (account_id) references account (account_id)
foreign key (delivery_id) references delivery (delivery_id));

create table order_item (
order_item_id bigint not null,
count integer not null,
Copy link
Member

Choose a reason for hiding this comment

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

count는 굳이 컬럼으로 넣지 않아도 count()연산으로 해결할 수 있을 것 같습니다

order_price integer not null,
item_id bigint,
order_id bigint,
primary key (order_item_id),
foreign key (item_id) references item (item_id),
foreign key (order_id) references orders (order_id));

create table delivery (
delivery_id bigint not null,
Copy link
Member

Choose a reason for hiding this comment

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

담당 배달원이 누구인지도 명시해줘야할것같습니다~

city varchar(255),
street varchar(255),
zip_code varchar(255),
status varchar(255),
primary key (delivery_id));






Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import me.jjeda.mall.accounts.Service.AccountService;
import me.jjeda.mall.accounts.domain.AccountRole;
import me.jjeda.mall.accounts.domain.Address;
import me.jjeda.mall.common.model.Address;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.common.TestDescription;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.jjeda.mall.accounts.common.BaseControllerTest;
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.domain.AccountRole;
import me.jjeda.mall.accounts.domain.Address;
import me.jjeda.mall.common.model.Address;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.accounts.repository.AccountRepository;
import me.jjeda.mall.common.TestDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.jjeda.mall.accounts.common.BaseControllerTest;
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.domain.AccountRole;
import me.jjeda.mall.accounts.domain.Address;
import me.jjeda.mall.common.model.Address;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.accounts.repository.AccountRepository;
import me.jjeda.mall.common.TestDescription;
Expand Down