#30 jpa basic: relation mapping
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
package com.hello.jpa.relationmapping.ex03;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.persistence.EntityTransaction;
|
||||||
|
import javax.persistence.Persistence;
|
||||||
|
|
||||||
|
public class JpaMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
|
||||||
|
|
||||||
|
EntityManager em = emf.createEntityManager();
|
||||||
|
|
||||||
|
EntityTransaction tx = em.getTransaction();
|
||||||
|
tx.begin();
|
||||||
|
|
||||||
|
try {
|
||||||
|
tx.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
tx.rollback();
|
||||||
|
} finally {
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
emf.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.hello.jpa.relationmapping.ex03.domain;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Category {
|
||||||
|
|
||||||
|
@Id @GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "PARENT_ID")
|
||||||
|
private Category parent;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "parent")
|
||||||
|
private List<Category> child = new ArrayList<>();
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
@JoinTable(
|
||||||
|
name = "CATEGORY_ITEM",
|
||||||
|
joinColumns = @JoinColumn(name = "CATEGORY_ID"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name = "ITEM_ID")
|
||||||
|
)
|
||||||
|
private List<Item> items = new ArrayList<>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.hello.jpa.relationmapping.ex03.domain;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.OneToOne;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Delivery {
|
||||||
|
|
||||||
|
@Id @GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String city;
|
||||||
|
private String street;
|
||||||
|
private String zipcode;
|
||||||
|
|
||||||
|
private DeliveryStatus status;
|
||||||
|
|
||||||
|
@OneToOne(mappedBy = "delivery")
|
||||||
|
private Order order;
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package com.hello.jpa.relationmapping.ex03.domain;
|
||||||
|
|
||||||
|
public enum DeliveryStatus {
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package com.hello.jpa.relationmapping.ex03.domain;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Item {
|
||||||
|
|
||||||
|
@Id @GeneratedValue
|
||||||
|
@Column(name = "item_id")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private int price;
|
||||||
|
private int stockQuantity;
|
||||||
|
|
||||||
|
@ManyToMany(mappedBy = "items")
|
||||||
|
private List<Category> categories = new ArrayList<>();
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(int price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStockQuantity() {
|
||||||
|
return stockQuantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStockQuantity(int stockQuantity) {
|
||||||
|
this.stockQuantity = stockQuantity;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package com.hello.jpa.relationmapping.ex03.domain;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@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<>();
|
||||||
|
|
||||||
|
public List<Order> getOrders() {
|
||||||
|
return orders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrders(List<Order> orders) {
|
||||||
|
this.orders = orders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCity(String city) {
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStreet() {
|
||||||
|
return street;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStreet(String street) {
|
||||||
|
this.street = street;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getZipcode() {
|
||||||
|
return zipcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setZipcode(String zipcode) {
|
||||||
|
this.zipcode = zipcode;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package com.hello.jpa.relationmapping.ex03.domain;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "orders")
|
||||||
|
public class Order {
|
||||||
|
|
||||||
|
@Id @GeneratedValue
|
||||||
|
@Column(name = "order_id")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "member_id")
|
||||||
|
private Member member;
|
||||||
|
|
||||||
|
@OneToOne
|
||||||
|
@JoinColumn(name = "DELIVERY_ID")
|
||||||
|
private Delivery delivery;
|
||||||
|
|
||||||
|
private LocalDateTime orderDate;
|
||||||
|
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
private OrderStatus status;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "order")
|
||||||
|
private List<OrderItem> orderItems = new ArrayList<>();
|
||||||
|
|
||||||
|
public void addOrderItem(OrderItem orderItem) {
|
||||||
|
orderItems.add(orderItem);
|
||||||
|
orderItem.setOrder(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Member getMember() {
|
||||||
|
return member;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMember(Member member) {
|
||||||
|
this.member = member;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getOrderDate() {
|
||||||
|
return orderDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderDate(LocalDateTime orderDate) {
|
||||||
|
this.orderDate = orderDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderStatus getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatus(OrderStatus status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package com.hello.jpa.relationmapping.ex03.domain;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class OrderItem {
|
||||||
|
|
||||||
|
@Id @GeneratedValue
|
||||||
|
@Column(name = "order_item_id")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "order_id")
|
||||||
|
private Order order;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "item_id")
|
||||||
|
private Item item;
|
||||||
|
|
||||||
|
private int orderPrice;
|
||||||
|
private int count;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Order getOrder() {
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrder(Order order) {
|
||||||
|
this.order = order;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item getItem() {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItem(Item item) {
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOrderPrice() {
|
||||||
|
return orderPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderPrice(int orderPrice) {
|
||||||
|
this.orderPrice = orderPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCount() {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCount(int count) {
|
||||||
|
this.count = count;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.hello.jpa.relationmapping.ex03.domain;
|
||||||
|
|
||||||
|
public enum OrderStatus {
|
||||||
|
|
||||||
|
ORDER, CANCEL
|
||||||
|
}
|
||||||
@@ -15,9 +15,15 @@
|
|||||||
<!-- <class>com.hello.jpa.relationmapping.ex01.domain.Member</class>-->
|
<!-- <class>com.hello.jpa.relationmapping.ex01.domain.Member</class>-->
|
||||||
<!-- <class>com.hello.jpa.relationmapping.ex01.domain.Order</class>-->
|
<!-- <class>com.hello.jpa.relationmapping.ex01.domain.Order</class>-->
|
||||||
<!-- <class>com.hello.jpa.relationmapping.ex01.domain.OrderItem</class>-->
|
<!-- <class>com.hello.jpa.relationmapping.ex01.domain.OrderItem</class>-->
|
||||||
<class>com.hello.jpa.relationmapping.ex02.domain.Member</class>
|
<!-- <class>com.hello.jpa.relationmapping.ex02.domain.Member</class>-->
|
||||||
<class>com.hello.jpa.relationmapping.ex02.domain.Team</class>
|
<!-- <class>com.hello.jpa.relationmapping.ex02.domain.Team</class>-->
|
||||||
<class>com.hello.jpa.relationmapping.ex02.domain.Locker</class>
|
<!-- <class>com.hello.jpa.relationmapping.ex02.domain.Locker</class>-->
|
||||||
|
<class>com.hello.jpa.relationmapping.ex03.domain.Category</class>
|
||||||
|
<class>com.hello.jpa.relationmapping.ex03.domain.Delivery</class>
|
||||||
|
<class>com.hello.jpa.relationmapping.ex03.domain.Item</class>
|
||||||
|
<class>com.hello.jpa.relationmapping.ex03.domain.Member</class>
|
||||||
|
<class>com.hello.jpa.relationmapping.ex03.domain.Order</class>
|
||||||
|
<class>com.hello.jpa.relationmapping.ex03.domain.OrderItem</class>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- 필수 속성 -->
|
<!-- 필수 속성 -->
|
||||||
|
|||||||
Reference in New Issue
Block a user