querydsl을 사용하는 도중
반환값에 hashtag를 같이 반환하려 했는데
public Slice<CommunityListResponse> searchBySlice(Long lastId, Pageable pageable) {
List<CommunityListResponse> results = jpaQueryFactory
.select(
Projections.constructor(
CommunityListResponse.class,
community.id,
community.title,
community.views,
community.likeCount,
community.comments.size().longValue(),
community.member.nickname,
Projections.constructor(
MemberInfoResponse.class,
community.member.id,
community.member.nickname,
community.member.profileImageUrl,
community.member.introduction
),
community.createdAt,
community.updatedAt,
Projections.list(
Projections.constructor(
Hashtag.class,
hashtag.word,
hashtag.id
hashtag.count
)
)
)
)
.from(community)
.leftJoin(community.hashtags, communityHashtag)
.leftJoin(communityHashtag.hashtag, hashtag)
.where(
ltId(lastId),
community.isDeleted.eq(false)
)
.orderBy(community.id.desc())
.limit(pageable.getPageSize() + 1)
.fetch();
return checkLastPage(pageable, results);
}
hashtag가 하나로 안묶이고 자꾸 따로 조회되어 아래와 같이 나왔다.
{
"id": 74,
"title": "왜안바뀔까요1",
"views": 0,
"likeCount": 0,
"comments": 0,
"nickName": "닉네임301",
"member": {
"profileImageUrl": null,
"introduction": "성장하는 개발자입니다.",
"id": 171,
"nickname": "닉네임301"
},
"createdAt": "2024-02-21T00:55:06",
"updatedAt": "2024-02-21T00:55:06",
"hashtags": [
{
"id": 68,
"word": "tag1",
"count": 7
}
]
},
{
"id": 74,
"title": "왜안바뀔까요1",
"views": 0,
"likeCount": 0,
"comments": 0,
"nickName": "닉네임301",
"member": {
"profileImageUrl": null,
"introduction": "성장하는 개발자입니다.",
"id": 171,
"nickname": "닉네임301"
},
"createdAt": "2024-02-21T00:55:06",
"updatedAt": "2024-02-21T00:55:06",
"hashtags": [
{
"id": 73,
"word": "tag2",
"count": 8
}
]
},
{
"id": 74,
"title": "왜안바뀔까요1",
"views": 0,
"likeCount": 0,
"comments": 0,
"nickName": "닉네임301",
"member": {
"profileImageUrl": null,
"introduction": "성장하는 개발자입니다.",
"id": 171,
"nickname": "닉네임301"
},
"createdAt": "2024-02-21T00:55:06",
"updatedAt": "2024-02-21T00:55:06",
"hashtags": [
{
"id": 77,
"word": "tagss",
"count": 7
}
]
},
{
"id": 74,
"title": "왜안바뀔까요1",
"views": 0,
"likeCount": 0,
"comments": 0,
"nickName": "닉네임301",
"member": {
"profileImageUrl": null,
"introduction": "성장하는 개발자입니다.",
"id": 171,
"nickname": "닉네임301"
},
"createdAt": "2024-02-21T00:55:06",
"updatedAt": "2024-02-21T00:55:06",
"hashtags": [
{
"id": 78,
"word": "tag23",
"count": 7
}
]
}
member가 DB에 존재하지 않는 경우에도 값을 커스텀해서 넣어주고싶었다.
.leftJoin(community.member)을 하여 해결하였다..
QueryDSL에서 기본적으로 null인 값은 필터링되므로 community.member가 null인 경우 해당 튜플이 필터링되어 결과에 포함되지 않는다. 만약 null인 값도 결과에 포함시키고 싶다면 몇 가지 방법을 고려할 수 있다.
Left Join 사용:
leftJoin을 사용하여 community.member와 관련된 필드들을 조인한다. 이렇게 하면 community에 속한 member가 null이더라도 결과에 포함될 수 있다.
List<Tuple> tuples = jpaQueryFactory
.select(
community.id,
community.title,
community.views,
community.likeCount,
community.comments.size().longValue(),
community.member.id,
community.member.nickname,
community.member.profileImageUrl,
community.member.introduction,
community.createdAt,
community.updatedAt
)
.from(community)
.leftJoin(community.member) // Left Join 사용
.where(
ltId(lastId),
community.isDeleted.eq(false)
)
.orderBy(community.id.desc())
.limit(pageable.getPageSize() + 1)
.fetch();
위의 코드에서 leftJoin(community.member)를 추가하여 community.member가 null이더라도 결과에 포함시킬 수 있다.