티스토리 뷰

JPA

[JPA] 연관관계 매핑 예제

Jaime.Lee 2022. 7. 3. 10:30

소스 코드는 여기 있습니다. (commit hash: 0bacabb)

> git clone https://github.com/lcalmsky/jpa
> git checkout 0bacabb

Overview

연관관계 매핑 예제를 구현합니다.

테이블 구조

객체 구조

구현

테이블 구조에서 확인했다시피 FK를 가지는 테이블은 orders, order_item 두 가지이고, 객체 기준으로는 Order, OrderItem이 연관관계의 주인이 됩니다.

따라서

  • OrderMember@ManyToOne으로 가지고, @JoinColumn(name = "member_id")을 사용해 join 할 수 있습니다.
  • OrderItemOrder@ManyToOne으로 가지고, @JoinColumn(name = "order_id")을 사용해 join 할 수 있습니다.

그리고 Member, Order는 각각 Order, OrderItem에 대해 반대 관계를 가집니다. 연관관계의 주인쪽으로 mappedBy 속성을 사용합니다.

  • MemberOrder@OneToMany로 가지고, mappedBy = "member"로 지정합니다.
  • OrderOrderItem@OneToMany로 가지고, mappedBy = "order"로 지정합니다.

소스 코드로 나타내면 다음과 같습니다.

/src/main/java/com/tistory/jaimenote/jpa/domain/entity/Member.java

package com.tistory.jaimenote.jpa.domain.entity;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.ToString.Exclude;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@ToString
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")
  @Exclude
  private List<Order> orders = new ArrayList<>();

}

/src/main/java/com/tistory/jaimenote/jpa/domain/entity/Order.java

package com.tistory.jaimenote.jpa.domain.entity;

import com.tistory.jaimenote.jpa.domain.support.OrderStatus;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
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.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.ToString.Exclude;

@Entity
@Table(name = "orders")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@ToString
public class Order {

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

  @ManyToOne
  @JoinColumn(name = "member_id")
  private Member member;

  @OneToMany(mappedBy = "order")
  @Exclude
  private List<OrderItem> orderItems = new ArrayList<>();

  private LocalDateTime orderDateTime;

  @Enumerated(EnumType.STRING)
  private OrderStatus status;

}

/src/main/java/com/tistory/jaimenote/jpa/domain/entity/OrderItem.java

package com.tistory.jaimenote.jpa.domain.entity;

import com.tistory.jaimenote.jpa.domain.support.OrderStatus;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
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.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.ToString.Exclude;

@Entity
@Table(name = "orders")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@ToString
public class Order {

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

  @ManyToOne
  @JoinColumn(name = "member_id")
  private Member member;

  @OneToMany(mappedBy = "order")
  @Exclude
  private List<OrderItem> orderItems = new ArrayList<>();

  private LocalDateTime orderDateTime;

  @Enumerated(EnumType.STRING)
  private OrderStatus status;

}

/src/main/java/com/tistory/jaimenote/jpa/domain/entity/Item.java

package com.tistory.jaimenote.jpa.domain.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@ToString
public class Item {
  @Id
  @GeneratedValue
  @Column(name = "item_id")
  private Long id;

  private String name;

  private Integer price;

  private Integer stockQuantity;

}

'JPA' 카테고리의 다른 글

[JPA] 상속관계 매핑(1): 상속 전략  (1) 2022.07.05
[JPA] 다양한 연관관계 매핑  (0) 2022.07.04
[JPA] 연관관계의 주인  (0) 2022.06.30
[JPA] 양방향 연관관계  (0) 2022.06.29
[JPA] 단방향 연관관계  (0) 2022.06.28
댓글