Toy 프로젝트에서 Inner Class로 Request DTO, Response DTO를 통합 관리하였습니다.
그러나 JPQL을 사용하면서 아래와 같이 오류가 발생하는 것을 확인하였습니다.
원인은 JPQL의 return 값으로 Inner class를 지정하면 class를 찾을 수 없다는 에러가 발생하는 것이었습니다.
JPQL을 사용 시에는 별도 QueryDto를 생성하고 해당 클래스를 return 해줘야 합니다.
org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate class
테스트 코드는 아래와 같습니다.
public class TestDto {
@Schema(description = "RequestDto")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class Request implements Serializable {
@Schema(description = "내부코드")
private Long testId;
@Schema(description = "항목코드")
private String itemCode;
@Schema(description = "금액")
private BigDecimal amt;
/* Dto -> Entity */
public TestEntity toEntity() {
return TestEntity.builder()
.id(this.testId)
.itemCode(this.itemCode)
.amt(this.amt)
.build();
}
}
@Schema(description = "ResponseDto")
@Getter
@AllArgsConstructor
public static class Response implements Serializable {
@Schema(description = "내부코드")
private Long testId;
@Schema(description = "항목코드")
private String itemCode;
@Schema(description = "금액")
private BigDecimal amt;
/* Entity -> Dto */
public Response(TestEntity response) {
this.commonId = response.getId();
this.itemCode = response.getItemCode();
this.amt = response.getAmt();
}
}
}
Toy 프로젝트를 진행해 보니 Inner Class가 통합 관리에 대한 장점도 있지만 코드의 복잡성 증가와 단점도 존재하여
DTO를 분리하는 것으로 결정하였습니다.
읽어주셔서 감사합니다.
반응형
최근댓글