양방향 매핑 예제
User Class : Orders class = 1 : N 관계임
User Class
@Table(name = "ORDERS")
public class User {
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "USER_SEQ_INDEX_GEN"
)
@Column(name = "USER_ID", unique = true)
private Long id;
@Column(unique = true)
private String name;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
private List<Orders> orders = new ArrayList<>();
public User changeName(String name){
this.name = name;
return this;
}
public void addOrders(Orders order){
orders.add(order);
}
}
Orders Class
@Table(name="USER")
public class Orders {
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "ORDERS_SEQ_INDEX_GEN"
)
@Column(name = "ID", unique = true)
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "AMOUNT")
private int amount;
@CreationTimestamp
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
@Column(name = "ORDER_DATE", updatable = false)
private LocalDateTime dateTime;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID")
private User user;
}
'JPA > JPA' 카테고리의 다른 글
영속성 전이(CASCADE) (0) | 2020.06.09 |
---|---|
즉시 로딩과 지연 로딩 (0) | 2020.06.09 |
객체와 테이블 연관관계 매핑 (0) | 2020.06.09 |
엔티티 매핑 (0) | 2020.06.09 |
영속성 컨텍스트 (0) | 2020.06.09 |