• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

상속관계 Entity간 조인

20.04.07 00:10 작성 조회수 294

0

안녕하세요.

고객 테이블을 상속관계를 이용하여 아래와 같이 설계를 하였습니다.(개인고객, 법인고객)

@Entity
@Getter @Setter
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "CUSTOMER_TYPE")
public abstract class Customer extends BaseEntity {

@Id @GeneratedValue
@Column(name = "CUSTOMER_ID")
private Long id;

private String name;

@Embedded
private Address address;

private int lineCount;

@Column(name = "CUSTOMER_TYPE", insertable = false, updatable = false)
private String customerType;

}
@Entity
@Getter @Setter
@DiscriminatorValue("COMPANY")
public class CompanyCustomer extends Customer{

private String vatCode;

private String owner;

private String companyPhone;

}
@Entity
@Getter @Setter
@DiscriminatorValue("PERSON")
public class PersonalCustomer extends Customer {

private String jumin;

private String handPhone;
}

그리고 고객을 위한 DTO도 생성을 했습니다.

@Data
public class CustomerDto {

@QueryProjection
public CustomerDto(Long id, String name, int lineCount, Address address, String customerType, String jumin, String handPhone, String vatCode, String owner, String companyPhone) {
this.id = id;
this.name = name;
this.lineCount = lineCount;
this.address = address;
this.customerType = customerType;
this.jumin = jumin;
this.handPhone = handPhone;
this.vatCode = vatCode;
this.owner = owner;
this.companyPhone = companyPhone;
}

private Long id;

private String name;

private int lineCount;

private Address address;

private String customerType;

private String jumin;

private String handPhone;

private String vatCode;

private String owner;

private String companyPhone;

}

위 상태에서 querydsl를 이용해서 DTO에 Data를 넣기 위해서는 querydsl을 어떻게 생성해야 할꺄요?

실제로 sql query는 

    select

        customer0_.customer_id as customer2_2_,

        customer0_.crt_dt as crt_dt3_2_,

        customer0_.updt_dt as updt_dt4_2_,

        customer0_.city as city5_2_,

        customer0_.street as street6_2_,

        customer0_.zipcode as zipcode7_2_,

        customer0_.customer_type as customer1_2_,

        customer0_.line_count as line_cou8_2_,

        customer0_.name as name9_2_,

        customer0_.company_phone as company10_2_,

        customer0_.owner as owner11_2_,

        customer0_.vat_code as vat_cod12_2_,

        customer0_.hand_phone as hand_ph13_2_,

        customer0_.jumin as jumin14_2_ 

    from

        customer customer0_

위와 같이 발생하는 방법으로 알고 싶습니다.

감사합니다.

답변 1

답변을 작성해보세요.

0

안녕하세요. 김문근님

이 경우는 엔티티로 조회하신 다음에 엔티티를 직접 DTO로 직접 변환하셔야 합니다.

그게 아니라면 네이티브 SQL을 사용하셔야 합니다.

만약 비즈니스 로직에 큰 차이가 없고, 단순히 데이터의 차이만 있다면, 상속 관계를 사용하지 말고, 한 테이블에 합치는 것을 권장합니다.

감사합니다.