#30 jpa basic: relation mapping ex
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package com.hello.jpa.relationmapping.ex01;
|
||||
|
||||
import com.hello.jpa.relationmapping.ex01.domain.Order;
|
||||
import com.hello.jpa.relationmapping.ex01.domain.OrderItem;
|
||||
|
||||
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 {
|
||||
Order order = new Order();
|
||||
order.addOrderItem(new OrderItem());
|
||||
|
||||
tx.commit();
|
||||
} catch (Exception e) {
|
||||
tx.rollback();
|
||||
} finally {
|
||||
em.close();
|
||||
}
|
||||
|
||||
emf.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.hello.jpa.relationmapping.ex01.domain;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Item {
|
||||
|
||||
@Id @GeneratedValue
|
||||
@Column(name = "item_id")
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
private int price;
|
||||
private int stockQuantity;
|
||||
|
||||
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.ex01.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,64 @@
|
||||
package com.hello.jpa.relationmapping.ex01.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;
|
||||
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.ex01.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.ex01.domain;
|
||||
|
||||
public enum OrderStatus {
|
||||
|
||||
ORDER, CANCEL
|
||||
}
|
||||
@@ -9,8 +9,12 @@
|
||||
<!-- <class>com.hello.jpa.entitymapping.ex01.domain.Member</class>-->
|
||||
<!-- <class>com.hello.jpa.entitymapping.ex01.domain.Order</class>-->
|
||||
<!-- <class>com.hello.jpa.entitymapping.ex01.domain.OrderItem</class>-->
|
||||
<class>com.hello.jpa.relationmapping.ex00.Member</class>
|
||||
<class>com.hello.jpa.relationmapping.ex00.Team</class>
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex00.Member</class>-->
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex00.Team</class>-->
|
||||
<class>com.hello.jpa.relationmapping.ex01.domain.Item</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.OrderItem</class>
|
||||
|
||||
<properties>
|
||||
<!-- 필수 속성 -->
|
||||
|
||||
Reference in New Issue
Block a user