프로젝트 세팅
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="hello">
<properties>
<!-- 필수 속성 -->
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="javax.persistence.jdbc.user" value="springjpa" />
<property name="javax.persistence.jdbc.password" value="springjpa" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL10Dialect" /> -->
<!-- 테이블 생성 옵션 -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<!-- 옵션 -->
<!-- 콘솔에 하이버네이트가 실행하는 SQL문 출력 -->
<property name="hibernate.show_sql" value="true" />
<!-- SQL 출력 시 보기 쉽게 정렬 -->
<property name="hibernate.format_sql" value="true" />
<!-- 쿼리 출력 시 주석(comments)도 함께 출력 -->
<property name="hibernate.use_sql_comments" value="true" />
</properties>
</persistence-unit>
</persistence>
시작
jpaitem.domain 패키지 생성
Item.java
생성@Entity
@Getter @Setter
public class Item {
@Id @GeneratedValue // 전략을 특정해주지 않으면 자동으로 생성 시퀀스를 생성한다.
@Column(name = "ITEM_ID")
private Long id;
private String name;
private int price;
private int stockQuantity;
}
Member.java
생성@Entity
@Getter @Setter
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
private String name;
private String city;
private String street;
private String zipcode;
}
Order.java
생성 @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;
}
OrderItem.java
생성@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;
}
JpaMain.java
생성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();
}
}
}
JpaMain.java
실행 시 )Order.java
수정@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;
@ManyToOne
@JoinColumn(name = "MEMBER_ID")
private Member member;
}
Member.java
수정@Entity
@Getter @Setter
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> order = new ArrayList<Order>();
public void addOrder(Order order) {
order.setMember(this);
this.order.add(order);
}
}
OrderItem.java
수정@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;
@ManyToOne
@JoinColumn(name = "ORDER_ID")
private Order order;
@ManyToOne
@JoinColumn(name = "ITEM_ID")
private Item item;
}
Order.java
수정...
@OneToMany(mappedBy= "order")
private List<OrderItem> orderItem = new ArrayList<OrderItem>();
public void addOrderItem(OrderItem orderItem) {
orderItem.setOrder(this);
this.orderItem.add(orderItem);
}
}
...
@OneToMany(mappedBy = "item")
private List<OrderItem> orderItem = new ArrayList<OrderItem>();
public void addOrderItem(OrderItem orderItem) {
orderItem.setItem(this);
this.orderItem.add(orderItem);
}
}
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();
}
}
}