๋ฐ์ํ
์๋ฐ ORM ํ์ค JPA ํ๋ก๊ทธ๋๋ฐ - ๊ธฐ๋ณธํธ์ ๊ณต๋ถํ๋ฉฐ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.
๐ํ ์ด๋ธ ๊ตฌ์กฐ
โ๊ฐ์ฒด ๊ตฌ์กฐ
์ด๋ ต๊ฒ ์๊ฐํ์ง๋ง๊ณ , ๋ค๋์ผ ๊ด๊ณ์์ '๋ค' ์ ํด๋นํ๋ ์ฐธ์กฐ๊ด๊ณ๋ @ManyToOne๊ณผ @JoinColumn(name="๊ธฐ๋ณธํค")๋ฅผ ์ฌ์ฉํ์ฌ ์ฐธ์กฐ๊ด๊ณ๋ฅผ ์ค์ ์์ผ์ค๋ค.
'์ผ' ๊ด๊ณ์์๋ @OneToMany(mappedBy ="์ธ๋ํค") mappedBy๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ํด์ค๋ค.
@Entity
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")
private List<Order> orders = new ArrayList<Order>();
// getter, setter
}
๐ด@Column(name = "MEMBER_ID")
// ์ ๋งคํ ์ปฌ๋ผ๋ง ์ด๋ฆ ๋งคํํด์ค๋ค. java์์๋ memberId / DB์์๋ member_id or MEMBER_ID ์ด๋ฐ์์ผ๋ก "_"๋ฅผ ์ ํธํ๋ ํธ์ด๋ค.
@Entity
public class Order {
@Id @GeneratedValue
@Column(name = "ORDER_ID") // **
private Long id;
@ManyToOne
@JoinColumn(name = "MEMBER_ID") //Member(PK)๋ก ๋งคํ
private Member member;
@OneToMany(mappedBy = "order")
List<OrderItem> orderItems = new ArrayList<OrderItem>();
private Date orderDate;
private OrderStatus status;
// getter, setter ์๋ต
}
@Entity
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;
// getter, setter ์๋ต
}
@Entity
public class Item{
@Id @GeneratedValue
private Long id;
private String name;
private int price;
private int stockQuantity;
// getter, setter ์๋ต
}
๋ฐ์ํ