-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…zation [REFACTOR] Querydsl을 이용한 쿼리 최적화
- Loading branch information
Showing
8 changed files
with
104 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
umbba-domain/src/main/java/sopt/org/umbba/domain/config/jpa/JpaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
package sopt.org.umbba.domain.config.jpa; | ||
|
||
import org.springframework.boot.autoconfigure.domain.EntityScan; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import sopt.org.umbba.domain.UmbbaDomainRoot; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
@Configuration | ||
@EntityScan(basePackageClasses = {UmbbaDomainRoot.class}) | ||
@EnableJpaRepositories(basePackageClasses = {UmbbaDomainRoot.class}) | ||
@EnableJpaAuditing | ||
public class JpaConfig { | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(em); | ||
} | ||
} |
85 changes: 41 additions & 44 deletions
85
umbba-domain/src/main/java/sopt/org/umbba/domain/domain/parentchild/dao/ParentchildDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,75 @@ | ||
package sopt.org.umbba.domain.domain.parentchild.dao; | ||
|
||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import com.querydsl.jpa.JPAExpressions; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Repository; | ||
import sopt.org.umbba.domain.domain.parentchild.Parentchild; | ||
import sopt.org.umbba.domain.domain.user.QUser; | ||
import sopt.org.umbba.domain.domain.user.User; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.NoResultException; | ||
import javax.persistence.PersistenceContext; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static sopt.org.umbba.domain.domain.parentchild.QParentchild.parentchild; | ||
import static sopt.org.umbba.domain.domain.user.QUser.user; | ||
|
||
@Slf4j | ||
@Repository | ||
@RequiredArgsConstructor | ||
public class ParentchildDao { | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
public Optional<Parentchild> findByUserId(Long userId) { | ||
|
||
String jpql = "SELECT pc FROM Parentchild pc " + | ||
"JOIN User u ON u.parentChild = pc " + | ||
"WHERE u.id = :id"; | ||
return Optional.ofNullable(queryFactory | ||
.selectFrom(parentchild) | ||
.leftJoin(user.parentChild, parentchild) | ||
.where( | ||
userIdEq(userId) | ||
) | ||
.fetchOne()); | ||
} | ||
|
||
try { | ||
Parentchild parentchild = em.createQuery(jpql, Parentchild.class) | ||
.setParameter("id", userId) | ||
.getSingleResult(); | ||
return Optional.ofNullable(parentchild); | ||
public Optional<User> findMatchUserByUserId(Long userId) { | ||
|
||
} catch (NoResultException e) { | ||
return Optional.empty(); | ||
} finally { | ||
em.close(); | ||
} | ||
QUser uc = new QUser("uc"); | ||
|
||
return Optional.ofNullable(queryFactory | ||
.select(user) | ||
.from(user) | ||
.join(uc).on(uc.parentChild.eq(user.parentChild)) | ||
.where(uc.id.eq(userId).and(uc.id.ne(user.id))) | ||
.fetchOne()); | ||
} | ||
|
||
public Optional<User> findMatchUserByUserId(Long userId) { | ||
public List<String> findFcmTokensById(Long parentchildId) { | ||
|
||
String jpql = "SELECT u FROM User u " + | ||
"JOIN User uc ON uc.parentChild = u.parentChild " + | ||
"WHERE uc.id = :id AND uc.id != u.id"; | ||
|
||
try { | ||
User user = em.createQuery(jpql, User.class) | ||
.setParameter("id", userId) | ||
.getSingleResult(); | ||
return Optional.ofNullable(user); | ||
} catch (NoResultException e) { | ||
return Optional.empty(); | ||
} finally { | ||
em.close(); | ||
} | ||
return queryFactory | ||
.select(user.fcmToken) | ||
.from(user) | ||
.leftJoin(user.parentChild, parentchild) | ||
.where( | ||
parentchildIdEq(parentchildId) | ||
) | ||
.fetch(); | ||
} | ||
|
||
public List<String> findFcmTokensById(Long parentchildId) { | ||
|
||
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; | ||
} | ||
|
||
private BooleanExpression parentchildIdEq(Long parentchildId) { | ||
return parentchildId != null ? parentchild.id.eq(parentchildId) : null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters