JPQL 동적쿼리 부분 - > QUERYDSL 마이그레이션 후 질문
513
작성한 질문수 8
querydsl설정은 각종 블로그 보고 설정 적용을 한상태입니다.
일단 엔티티 구조가
Transaction 엔티티에서

Accout 엔티티를 @MantToOne 다대일 단방향 매핑중이고,
입출금 내역 조회부분에서 jpql 부분을 그대로 챗 gpt 에 querydsl 기준 동적쿼리 코드를 돌려보면

이런 코드를 알려줍니다 우선 여기서
질문이
WithdrawAccount, DepositAccount라는 클래스는 없고 Account를 참조 하는데 QWithdrawAccount, QDepositAccount Q클래스가 필요한 이유를 모르겠습니다.
저 코드를

해당 코드와 같이 바꿨습니다 그리고 테스트 코드를 돌려보면

transaction.withdrawAccount is not a root path 라는 에러가 나는데
root path 관련 질문글 찾아보면
" 이를 해결하려면 from 절에서 해당 객체를 다시 선언해주면 됩니다. 즉, querydsl에서는 join을 할 때 alias를 줘서 해당 객체를 다시 선언해주는 방식으로 작성해야 해결할 수 있습니다. "
이런 답변이있는데
변경 된 코드에서 innerjoin의 오른쪽 파라미터에 별칭을 잘못 줘서 에러가 나는거 같긴 한데
QTransaction.transaction.withdrawaccount 와 같은 별칭을 부여하면 안되는 걸까요,,?
답변 2
0
혹시 fetchjoin 적용 안된거 같은데 할필요가 없는걸까요?
public List<Transaction> findTransactionList(Long accountId, String gubun, Integer page){
JPAQuery<Transaction> query = jpaQueryFactory.selectFrom(transaction);
return query
.leftJoin(transaction.withdrawAccount).leftJoin(transaction.depositAccount)
.where(gubunCheck(gubun, accountId))
.limit(3).offset(page*3)
.fetch();
}
private BooleanExpression gubunCheck(String gubun, Long accountId){
if (!StringUtils.hasText(gubun)){
return
transaction.withdrawAccount.id.eq(accountId).or(transaction.depositAccount.id.eq(accountId));
}else if(TransactionEnum.valueOf(gubun) == TransactionEnum.DEPOSIT){
return
fetchjtransaction.depositAccount.id.eq((accountId));
}else if(TransactionEnum.valueOf(gubun) == TransactionEnum.WITHDRAW){
return
transaction.withdrawAccount.id.eq(accountId);
}else {
return null;
}
}
0
@Override
public List<Transaction> findTransactionList(Long accountId, String gubun, Integer page) {
JPAQuery<Transaction> query = jpaQueryFactory.selectFrom(transaction);
if ("WITHDRAW".equals(gubun)) {
query
.innerJoin(transaction.withdrawAccount, QAccount.account)
.fetchJoin()
.where(transaction.withdrawAccount.id.eq(accountId));
} else if ("DEPOSIT".equals(gubun)) {
query
.innerJoin(transaction.depositAccount, QAccount.account)
.fetchJoin()
.where(transaction.depositAccount.id.eq(accountId));
} else { // gubun = alls
query
.leftJoin(transaction.withdrawAccount, QAccount.account)
.leftJoin(transaction.depositAccount, QAccount.account)
.where(transaction.withdrawAccount.id.eq(accountId)
.or(transaction.depositAccount.id.eq(accountId)));
}
return query
.offset(page * 5)
.limit(5)
.fetch();
}혹시 booleanexpression을 써야하나요?? 그리고 보여주신 링크는 좀 쿼리 자체가 달라서 jpql 쿼리부터,,
0


이렇게 하니 되네요,,? QAccout Q클래스가 맞았나 봅니다..,,
QAccount withdrawAccount = QAccount.account;
QAccount depositAccount = QAccount.account;
0
쿼리 DSL 적용된 커밋 로그입니다.
git clone 받아서
git reset --hard 0456
하면 코드 확인가능합니다.
요청/응답 DTO 관련 문의
0
172
2
안녕하세요 인증이 필요한 url을 위하여 /s를 붙이는것에 대해 질문있습니다.
0
161
1
validation aop사용에 대해서 질문있습니다.
0
246
2
Dummy 클래스 위치에 대한 질문
0
294
2
테스트 방식에 관해서 질문이 있어요
0
293
2
스프링 버전업일 경우에는 Pointcut @PostMapping 조건이 달라질까요?
1
435
1
equals와 longValue 관련 질문드립니다
0
330
1
계좌번호를 Long 타입으로 하는 이유가 무엇일까요?!
0
500
2
[정보공유] Hibernate 로그 작동 안하시는 분들!!
3
346
0
UserControllerTest 테스트 실패 문의
0
319
1
스프링 시큐리티 6.2 버전 이후로 apply() 메서드를 이용한 JwtAuthenticationFilter 가 등록이 안됩니다.
2
1108
1
import 오류
0
428
3
spring initializer gradle 에서 3.x.x 대 밖에 없어요. 2.x.x는 보이지 않는데 어떡하져
0
440
2
안녕하세요 로그엔 성공적으로 들어온것같습니다..
0
237
1
JwtAuthorizationfilter test mvc.performget 관련 질문입니다!
0
288
1
JwtAuthorizationfilter test mvc.performget 부
0
229
1
longValue() 질문
0
231
1
jwt 인가필터 규현및 등록
0
334
1
스프링부트 3버전
1
335
1
권한처리를 위한 세션강제주입
0
418
1
JwtVO 를 인터페이스로 만든 이유
0
360
1
계좌 조회 질문드립니다
0
230
1
DummyObject 에 대하여
0
310
2
DTO를 이너클래스로 계속추가하는 이유
0
699
2





