#30 jpa basic: entity mapping ex

This commit is contained in:
haerong22
2023-01-14 21:52:03 +09:00
parent e78fc39151
commit 2c750fca68
8 changed files with 266 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
@Entity
//@Entity
@Table(name = "Member2")
public class Member {

View File

@@ -0,0 +1,31 @@
package com.hello.jpa.entitymapping.ex01;
import com.hello.jpa.entitymapping.ex00.Member;
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();
}
}

View File

@@ -0,0 +1,50 @@
package com.hello.jpa.entitymapping.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;
}
}

View File

@@ -0,0 +1,59 @@
package com.hello.jpa.entitymapping.ex01.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@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;
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;
}
}

View File

@@ -0,0 +1,52 @@
package com.hello.jpa.entitymapping.ex01.domain;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@Table(name = "orders")
public class Order {
@Id @GeneratedValue
@Column(name = "order_id")
private Long id;
@Column(name = "member_id")
private Long memberId;
private LocalDateTime orderDate;
@Enumerated(EnumType.STRING)
private OrderStatus status;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getMemberId() {
return memberId;
}
public void setMemberId(Long memberId) {
this.memberId = memberId;
}
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;
}
}

View File

@@ -0,0 +1,63 @@
package com.hello.jpa.entitymapping.ex01.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class OrderItem {
@Id @GeneratedValue
@Column(name = "order_item_id")
private Long id;
@Column(name = "order_id")
private Long orderId;
@Column(name = "item_id")
private Long itemId;
private int orderPrice;
private int count;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public Long getItemId() {
return itemId;
}
public void setItemId(Long itemId) {
this.itemId = itemId;
}
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;
}
}

View File

@@ -0,0 +1,6 @@
package com.hello.jpa.entitymapping.ex01.domain;
public enum OrderStatus {
ORDER, CANCEL
}

View File

@@ -5,6 +5,10 @@
<persistence-unit name="hello">
<class>com.hello.jpa.context.Member</class>
<class>com.hello.jpa.entitymapping.ex00.Member</class>
<class>com.hello.jpa.entitymapping.ex01.domain.Item</class>
<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>
<properties>
<!-- 필수 속성 -->
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>