Spring Boot JPA - 테이블 설계

UnKnown12·2022년 12월 7일
0

Spring Boot

목록 보기
5/13

📌 Item Entity

  • @Id = Primary Key
  • @GeneratedValue = 시퀀스 적용
@Entity
@Getter @Setter
public class Item {

		@Id @GeneratedValue 	// Id 에 Pk 값 + 
		@Column(name = "ITEM_ID")
		private long id;
		private String name;
		private int price;
		private int stockQuantity;
		
	
}

📌 Member Entity

  • 회원 가입 및 회원정보 조회 담당
@Entity
@Getter @Setter
public class Member {

	@Id @GeneratedValue
	@Column(name = "MEMBER_ID")
	private Long id;
	private String name;
	private Long city;
	private Long street;
	private Long zipcode;
	
}

📌 Order Entity

  • 사용자(고객) 의 요청(주문) 을 담당
  • Member 1명으로 N개의 Order 를 요청할 수 있다.
@Entity
@Table(name = "ORDERS")
@Getter @Setter
public class Order {

	@Id @GeneratedValue
	@Column(name = "ORDER_ID")
	private Long id;
    @Column(name = "MEMBER_ID")
	private Long memberId;
	private LocalDateTime orderDate;
	private String status;
	
	
}

📌 Order Item Entity

@Entity
@Getter @Setter
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;
}

📌 JPA Main 기본 셋팅

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();
		}
		
	}
}
profile
Hyobin12

0개의 댓글