JPA Entity를 구현하려면 다음 규칙을 지켜져야한다.
@Entity
@Table(name="member")
public class Member implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String mbrId;
private String name;
//Getter, Setter
}
복합키(EmbeddedId, @IdClass)
두 어노테이션은 물리적 모델 관점에서 차이점은 없다.
@IdClass에는 식별자 클래스를 생성해야한다. 예시에서는 PaymentMasterPk 클래스를 생성해주었다. 여기서 식별자 클래스에는 몇가지 규칙이 있다.
@Entity
@IdClass(PaymentMasterPK.class)
@Table(name="payment_mst")
public class PaymentMaster implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private String pmtCode;
@Id
private String pmtType;
private String pmtName;
private String partCnclYn;
// Getter, Setter
}
public class PaymentMasterPK implements Serializable{
private static final long serialVersionUID = 1L;
private String pmtCode;
private String pmtType;
public PaymentMasterPK(){
}
public PaymentMasterPK(String pmtCode, String pmtType){
if(StringUtils.isEmpty(pmtType)) {
pmtType = new String("");
}
this.pmtCode = pmtCode;
this.pmtType = pmtType;
}
// Getter, Setter
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(obj == null || this.getClass() != obj.getClass()) {
return false;
}
PaymentMasterPK paymentMasterPK = (PaymentMasterPK)obj;
if(this.pmtCode.equals(paymentMasterPK.pmtCode) && this.pmtType.equals(paymentMasterPK.pmtType) ) {
return true;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(pmtCode, pmtType);
}
}
@Entity
@Table(name="payment_mst")
public class PaymentMaster implements Serializable{
private static final long serialVersionUID = 1L;
@EmbeddedId
private PaymentMasterPk paymentMasterPK;
private String pmtName;
private String partCnclYn;
// Getter, Setter
}
@Embeddable
public class PaymentMasterPK implements Serializable{
private static final long serialVersionUID = 1L;
private String pmtCode;
private String pmtType;
public PaymentMasterPK(){
}
public PaymentMasterPK(String pmtCode, String pmtType){
this.pmtCode = pmtCode;
this.pmtType = pmtType;
}
// Getter, Setter
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(obj == null || this.getClass() != obj.getClass()) {
return false;
}
PaymentMasterPK paymentMasterPK = (PaymentMasterPK)obj;
if(this.pmtCode.equals(paymentMasterPK.pmtCode) && this.pmtType.equals(paymentMasterPK.pmtType) ) {
return true;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(pmtCode, pmtType);
}
}