Skip to content

Commit

Permalink
Merge pull request #116 from Team-Umbba/fix/#113-querydsl_error
Browse files Browse the repository at this point in the history
[FIX] jpql 코드로 복구
  • Loading branch information
ddongseop authored Oct 24, 2023
2 parents a58872b + e4d7266 commit 8dc2c7c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -31,13 +32,29 @@ public class ParentchildDao {

public Optional<Parentchild> findByUserId(Long userId) {

return Optional.ofNullable(queryFactory
.selectFrom(parentchild)
.leftJoin(user.parentChild, parentchild)
.where(
userIdEq(userId)
)
.fetchOne());
// return Optional.ofNullable(queryFactory
// .selectFrom(parentchild)
// .leftJoin(user.parentChild, parentchild)
// .where(
// userIdEq(userId)
// )
// .fetchOne());

String jpql = "SELECT pc FROM Parentchild pc " +
"JOIN User u ON u.parentChild = pc " +
"WHERE u.id = :id";

try {
Parentchild parentchild = em.createQuery(jpql, Parentchild.class)
.setParameter("id", userId)
.getSingleResult();
return Optional.ofNullable(parentchild);

} catch (NoResultException e) {
return Optional.empty();
} finally {
em.close();
}
}

public Optional<User> findMatchUserByUserId(Long userId) {
Expand Down Expand Up @@ -74,16 +91,29 @@ public Optional<User> findMatchUserByUserId(Long userId) {

public List<String> findFcmTokensById(Long parentchildId) {

return queryFactory
.select(user.fcmToken)
.from(user)
.leftJoin(user.parentChild, parentchild)
.where(
parentchildIdEq(parentchildId)
)
.fetch();
}
// return queryFactory
// .select(user.fcmToken)
// .from(user)
// .leftJoin(user.parentChild, parentchild)
// .where(
// parentchildIdEq(parentchildId)
// )
// .fetch();

String jpql = "SELECT u.fcmToken FROM User u " +
"JOIN Parentchild pc ON pc.id = u.parentChild.id " +
"WHERE pc.id = :id";

try {
return em.createQuery(jpql, String.class)
.setParameter("id", parentchildId)
.getResultList();
} catch (NoResultException e) {
return new ArrayList<>();
} finally {
em.close();
}
}

private BooleanExpression userIdEq(Long userId) {
return userId != null ? user.id.eq(userId) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,36 @@ public class QnADao {
// READ

// 유저 아이디로 QnA 리스트 조회하기
public List<QnA> findQnASByUserId(Long userId) {
return queryFactory
.select(parentchild.qnaList)
.from(parentchild)
.leftJoin(user.parentChild, parentchild)
.where(userIdEq(userId))
.fetchOne();
public Optional<List<QnA>> findQnASByUserId(Long userId) {
// return queryFactory
// .select(parentchild.qnaList)
// .from(parentchild)
// .leftJoin(user.parentChild, parentchild)
// .where(userIdEq(userId))
// .fetchOne();

log.info("jpql 실행 전");
String jpql = "SELECT q FROM Parentchild pc " +
"JOIN pc.qnaList q " +
"LEFT JOIN User u ON u.parentChild.id = pc.id " +
"WHERE u.id = :id";

try {
TypedQuery<QnA> query = em.createQuery(jpql, QnA.class);

log.info("query 실행 성공: {}", query);
List<QnA> qnaList = query
.setParameter("id", userId)
.getResultList();
log.info("query 실행 결과: {}", qnaList.toString());

return Optional.of(qnaList);
} catch (NoResultException e) {

return Optional.empty();
} finally {
em.close();
}
}

private BooleanExpression userIdEq(Long userId) {
Expand Down

0 comments on commit 8dc2c7c

Please sign in to comment.