JPA Entity

JPA Entity๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๊ทœ์น™์„ ์ง€์ผœ์ ธ์•ผํ•œ๋‹ค.

  1. ๋ชจ๋“  Entity ํด๋ž˜์Šค๋Š” @Id ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

    • ์ด๋•Œ @Id๋กœ ์„ค์ •๋œ ๊ฐ’์—๋Š” null๊ฐ’์ด ์˜ฌ ์ˆ˜ ์—†๋‹ค.

  2. ๊ธฐ๋ณธํ‚ค๊ฐ€ ๋ณตํ•ฉํ‚ค์ธ ๊ฒฝ์šฐ์—๋Š” @EmbeddedId ํ˜น์€ @IdClass๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

  3. ํ…Œ์ด๋ธ”์— Primary Key๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” Uniqueํ•œ ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ @Id annotation์„ ๋ถ™์ด๋ฉด๋œ๋‹ค.

๋‹จ์ผํ‚ค

@Entity
@Table(name="member")
public class Member implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String mbrId;

    private String name;

    //Getter, Setter
}
  • @Entity : JPA Entity์ž„์„ ์•Œ๋ฆฐ๋‹ค.

  • @Table : 'member' ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘๋จ์„ ๋ช…์‹œํ•œ๋‹ค.

  • @Id : Primary Key์ž„์„ ๋ช…์‹œํ•œ๋‹ค.

๋ณตํ•ฉํ‚ค(EmbeddedId, @IdClass)

๋‘ ์–ด๋…ธํ…Œ์ด์…˜์€ ๋ฌผ๋ฆฌ์  ๋ชจ๋ธ ๊ด€์ ์—์„œ ์ฐจ์ด์ ์€ ์—†๋‹ค.

  • @EmbeddedId๋Š” ๊ฒฐํ•ฉ ๋œ PK๊ฐ€ ์˜๋ฏธ์žˆ๋Š” ์—”ํ‹ฐํ‹ฐ ์ž์ฒด์ด๊ฑฐ๋‚˜ ์ฝ”๋“œ์—์„œ ์žฌ์‚ฌ์šฉ ๋  ๋•Œ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค.

    • ๊ฐ์ฒด ์ง€ํ–ฅ์— ๋” ๊ฐ€๊นŒ์šด ๋ฐฉ๋ฒ•

  • @IdClass๋Š” ํ•„๋“œ์˜ ์ผ๋ถ€ ์กฐํ•ฉ์ด ๊ณ ์œ ํ•˜์ง€๋งŒ ํŠน๋ณ„ํ•œ ์˜๋ฏธ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜๋‹ค.

    • DB์— ๋” ๊ฐ€๊นŒ์šด ๋ฐฉ๋ฒ•

@IdClass

@IdClass์—๋Š” ์‹๋ณ„์ž ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค. ์˜ˆ์‹œ์—์„œ๋Š” PaymentMasterPk ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์—ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‹๋ณ„์ž ํด๋ž˜์Šค์—๋Š” ๋ช‡๊ฐ€์ง€ ๊ทœ์น™์ด ์žˆ๋‹ค.

  1. Serializable ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„

  2. ์‹๋ณ„์ž ํด๋ž˜์Šค์˜ ํ•„๋“œ๋ช…๊ณผ Entity์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์‹๋ณ„์ž์˜ ํ•„๋“œ๋ช…์ด ๋™์ผํ•ด์•ผํ•œ๋‹ค.

  3. equals() ์™€ hashCode() ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค.

  4. ๊ธฐ๋ณธ ์ƒ์„ฑ์ž(args๊ฐ€ ์—†๋Š”)๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค.

  5. ํด๋ž˜์Šค์˜ ์ ‘๊ทผ ์ œํ•œ์ž๋Š” public์ด์–ด์•ผํ•œ๋‹ค.

@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);        
    }
}

@EmbeddedId

  1. ์‹๋ณ„์ž ํด๋ž˜์Šค์— @Embeddable์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

  2. Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค.

  3. equals(), hashCode() ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค.

  4. ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธํ•ด์•ผํ•œ๋‹ค.

  5. ํด๋ž˜์Šค์˜ ์ ‘๊ทผ ์ œํ•œ์ž๋Š” public์ด์–ด์•ผํ•œ๋‹ค.

@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);        
    }
}

์ฐธ์กฐ

Last updated