-
Notifications
You must be signed in to change notification settings - Fork 151
[4기 황창현, 이현호] JPA Mission 3 - 도메인 연관관계 매핑 실습 #302
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
base: 창현,현호-mission2
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uCC3D\uD604,\uD604\uD638-mission3"
Changes from all commits
e9efc1d
25fd6b6
d9b65f4
710b5e5
bf23613
ae6d950
5af566a
9f20261
07a953e
4b0946b
79abaee
358544f
a34dee8
737fa92
f5c3dc7
39c94fe
a1af086
c64bb48
e3a73f6
c6668a7
e136bc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.programmers.springbootjpa.domain.item; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Table(name = "item") | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Item { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
private Long id; | ||
private String name; | ||
private Integer price; | ||
private Integer stockQuantity; | ||
|
||
@Builder | ||
public Item(String name, Integer price, Integer stockQuantity) { | ||
this.name = name; | ||
this.price = price; | ||
this.stockQuantity = stockQuantity; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.programmers.springbootjpa.domain.order; | ||
|
||
import com.programmers.springbootjpa.domain.customer.Customer; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import java.time.LocalDateTime; | ||
import java.util.Objects; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Table(name = "orders") | ||
@Getter | ||
public class Order { | ||
|
||
@Id | ||
@Column(name = "id", nullable = false) | ||
private String uuid; | ||
|
||
@Column(name = "order_datetime", columnDefinition = "TIMESTAMP", nullable = false) | ||
private LocalDateTime orderDatetime; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name = "order_status", nullable = false) | ||
private OrderStatus orderStatus; | ||
|
||
@Column(name = "memo", columnDefinition = "TEXT") | ||
private String memo; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "customer_id", referencedColumnName = "id") | ||
private Customer customer; | ||
|
||
@Builder | ||
public Order(String uuid, LocalDateTime orderDatetime, OrderStatus orderStatus, String memo) { | ||
this.uuid = uuid; | ||
this.orderDatetime = orderDatetime; | ||
this.orderStatus = orderStatus; | ||
this.memo = memo; | ||
} | ||
|
||
public void setCustomer(Customer customer) { | ||
if (Objects.nonNull(this.customer)) { | ||
this.customer.remove(this); | ||
} | ||
|
||
this.customer = customer; | ||
customer.add(this); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이쪽 부분에도 OrderItem연관관계 편의 메소드에서 사용할 add,remove 메소드를 만들 수 있을 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Order역시 OrderItem과의 연관 관계는 단방향이라서 편의 메서드를 만들지 않았습니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.programmers.springbootjpa.domain.order; | ||
|
||
import com.programmers.springbootjpa.domain.item.Item; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@Table(name = "order_item") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class OrderItem { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
private Long id; | ||
|
||
private Integer price; | ||
|
||
private Integer quantity; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "order_id", referencedColumnName = "id") | ||
private Order order; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "item_id", referencedColumnName = "id") | ||
private Item item; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Order, Item 클래스의 연관관계 편의 메소드를 만들어주세요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 단방향이라서 편의 메서드는 만들 수 없었습니다. |
||
@Builder | ||
public OrderItem(Integer price, Integer quantity, Order order, Item item) { | ||
this.price = price; | ||
this.quantity = quantity; | ||
this.order = order; | ||
this.item = item; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.programmers.springbootjpa.domain.order; | ||
|
||
public enum OrderStatus { | ||
OPENED, | ||
CANCELLED | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.programmers.springbootjpa.repository; | ||
|
||
import com.programmers.springbootjpa.domain.item.Item; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ItemRepository extends JpaRepository<Item, Long> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.programmers.springbootjpa.repository; | ||
|
||
import com.programmers.springbootjpa.domain.order.OrderItem; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface OrderItemRepository extends JpaRepository<OrderItem, Long> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.programmers.springbootjpa.repository; | ||
|
||
import com.programmers.springbootjpa.domain.order.Order; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface OrderRepository extends JpaRepository<Order, String> { | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Order 클래스의 리뷰를 참고하여 연관관계 편의 메소드에서 사용할 add,remove 메소드를 만들수 있을 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Item의 경우 단방향 연관관계로 매핑되어 있어서 편의 메서드를 쓸 수 없다고 생각했습니다!