IT/Spring
JPA Multi Column Join
민쌍
2020. 6. 13. 01:25
Legacy DB의 JPA Entity Mapping (복합키 매핑 편) - 우아한형제들 기술 블로그
Header - One
@Getter @
@Getter
@Entity
@Table(name = "pay_detail_2")
@IdClass(PayDetailId2.class)
@NoArgsConstructor
public class PayDetail2 {
@Id
private Long payNumber;
@Id
private Long paySeq;
@Id
private Long payDetailId;
@OneToMany(mappedBy = "payDetail2", fetch = FetchType.LAZY)
@org.hibernate.annotations.ForeignKey(name = "none")
private List<PayShop2> payShops = new ArrayList<>();
}
Detail - Many
@Getter
@Entity
@Table(name = "pay_shop_2")
@IdClass(PayShopId2.class)
@NoArgsConstructor
public class PayShop2 {
@Id
private Long payNumber;
@Id
private Long paySeq;
@Id
private Long payDetailId;
@Id
private String shopNumber;
private String shopName;
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumns(value = {
@JoinColumn(name = "payNumber", updatable = false, insertable = false),
@JoinColumn(name = "paySeq", updatable = false, insertable = false),
@JoinColumn(name = "payDetailId", updatable = false, insertable = false)
}, foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
private PayDetail2 payDetail2;
}
PayDetailId2.class
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@NoArgsConstructor
public class PayDetailId2 implements Serializable {
@EqualsAndHashCode.Include
@Id
private Long payNumber;
@EqualsAndHashCode.Include
@Id
private Long paySeq;
@EqualsAndHashCode.Include
@Id
private Long payDetailId;
}
PayShopId2.class
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@NoArgsConstructor
public class PayShopId2 implements Serializable {
@EqualsAndHashCode.Include
@Id
private Long payNumber;
@EqualsAndHashCode.Include
@Id
private Long paySeq;
@EqualsAndHashCode.Include
@Id
private Long payDetailId;
@EqualsAndHashCode.Include
@Id
private String shopNumber;
}
2개 이상의 Columns Table Join
Compile Error Log
클래스명 @IdClass 사용시
@IdClass(PayShop2.class)
@JoinTable(name = "PO_DETAIL",
joinColumns = {
@JoinColumn(name = "CO_GB", referencedColumnName="CO_GB", insertable = false, updatable = false),
@JoinColumn(name = "PO_NO", referencedColumnName="PO_NO", insertable = false, updatable = false)
})
아래와 같은 hibernate, JPA complie error
A component cannot hold properties split into 2 different tables:
a foreign key refering has the wrong number of column
728x90
반응형