๐Ÿ”ญQuery DSL

๋ฐ•์šฉ๋ฏผยท2024๋…„ 3์›” 17์ผ
0
post-thumbnail

Spring ๊ณต๋ถ€ ๋ฐ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋ฉด์„œ DB์— ๋งคํ•‘ํ•˜๋Š” ๊ธฐ์ˆ ๋กœ JPA๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜์˜€๋‹ค.
ํŠนํžˆ ๋งŽ์€ CRUD์„ JPA๋กœ ์‚ฌ์šฉํ•˜์˜€๊ณ  ๊ฐ์ฒด ์ง€ํ–ฅ์ ์ธ ๋ฐฉ์‹ ๋•๋ถ„์— SQL์— ํˆฌ์žํ•˜๋Š” ์‹œ๊ฐ„์„ ์„œ๋น„์Šค ๋กœ์ง์— ๊ฐœ๋ฐœํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์—”ํ‹ฐํ‹ฐ ์—ฌ๋Ÿฌ๊ฒŒ๋ฅผ ๋งŒ๋“ค๊ณ  ์„œ๋กœ ํ…Œ์ด๋ธ” ๋ผ๋ฆฌ ์กฐ์ธ์„ ํ•  ์ˆ˜๋ก ๋ณต์žกํ•˜๊ณ  ๊ธธ์–ด์ง€๋Š” ํ•จ์ˆ˜๋ช…์„ ๋งŒ๋“ค ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.
JPA๋กœ ๋งŒ์•ฝ์— ํŒŒํ‹ฐ์˜ ์ œ๋ชฉ ๋˜๋Š” ๊ฒŒ์ž„ ์ด๋ฆ„์œผ๋กœ ๊ฒ€์ƒ‰์„ ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๋ช…์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

Party findPartyByTitleOrGameTitle(String title, String gameTitle);

or 

@Query("SELECT p FROM Party p WHERE p.title = :title OR p.gameTitle = :gameTitle")
Party findParty(String title, String gameTitle);

์ด๋Ÿฐ ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ๊ฐ์ฒด ์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด์ธ JPQL์„ ์‚ฌ์šฉํ•˜์—ฌ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ”ฌJPQL(Java Persistence Query Language)

JPA๋Š” SQL์„ ์ถ”์ƒํ™”ํ•œ ๊ฐ์ฒด ์ฟผ๋ฆฌ ์–ธ์–ด๋กœ JPQL์„ ์ œ๊ณตํ•œ๋‹ค.

SELECT e FROM Employee e JOIN e.department d WHERE d.name = :departmentName
public Party findPartyByTitleOrGameTitle(String title, String gameTitle) {
        String jpql = "SELECT p FROM Party p WHERE p.title = :title OR p.gameTitle = :gameTitle";
        TypedQuery<Party> query = entityManager.createQuery(jpql, Party.class);
        query.setParameter("title", title);
        query.setParameter("gameTitle", gameTitle);
        List<Party> results = query.getResultList();
        return results.isEmpty() ? null : results.get(0);
    }

๊ทธ๋Ÿฌ๋‚˜ ํ•ด๋‹น ์ฝ”๋“œ์—๋„ ๋‹จ์ ์ด ์žˆ๋Š”๋ฐ ๋ฉ”์„œ๋“œ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ null์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  JPQL ์ฟผ๋ฆฌ๊ฐ€ ๋ฉ”์„œ๋“œ ๋‚ด์— ํ•˜๋“œ ์ฝ”๋”ฉ๋˜์–ด ์žˆ๋‹ค.

์ด์ค‘ JPQL์˜ ํ•˜๋“œ ์ฝ”๋”ฉ์— ๋ฌธ์ œ์ ์— ๋Œ€ํ•ด์„œ ์ง€์ ํ•˜์ž๋ฉด

  • ์ฟผ๋ฆฌ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋งค๋ฒˆ ๋ฒˆ๊ฑฐ๋Ÿฝ๊ฒŒ ์ฝ”๋“œ ์ˆ˜์ •์„ ํ•˜์—ฌ ์œ ์ง€ ๋ณด์ˆ˜์˜ ์–ด๋ ค์›€์ด ์žˆ๋‹ค.
  • ์ฝ”๋“œ์˜ ๊ธธ์ด๊ฐ€ ๋ณต์žกํ•ด์ง€๋ฉด ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ณ  ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค.
  • ์ฟผ๋ฆฌ ์˜ค๋ฅ˜๊ฐ€ ์–ด๋ ค์›Œ ๋Ÿฐํƒ€์ž„ ์‹œ์ ์— ์˜ค๋ฅ˜๋ฅผ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ฟผ๋ฆฌ๋ฅผ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์กฐํ•ฉํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

์ด์ค‘ 3,4๋ฒˆ์งธ ์ด์œ ๋ฅผ ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐ๋œ๋‹ค.

๋จผ์ € ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋ฉด์„œ ์˜ค๋ฅ˜๋ฅผ ํ”ผํ•˜๊ณ  ์‹ถ์–ด๋„ ์ƒ๊ธธ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค. ์˜ค๋ฅ˜์—๋„ ์ข‹์€ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๊ณ  ๋‚˜์œ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๊ฐ€์žฅ ์ข‹์€ ์˜ค๋ฅ˜๋Š” ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ์ฆ‰ ๋นŒ๋“œ๋ฅผ ํ•˜๊ธฐ์ „์— ๋ฌธ์ œ์ ์„ ํŒŒ์•…ํ•˜์—ฌ ์‹คํ–‰๋˜๊ธฐ ์ „ ์˜ค๋ฅ˜๋ฅผ ๊ฒ€์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๊ฐ€์žฅ ์ข‹์€ ์˜ค๋ฅ˜์ด๋‹ค. ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๋‚˜ ๋ฐฐํฌ๋ฅผ ํ–ˆ์„๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์˜ˆ๊ธฐ์น˜ ์•Š์€ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ณ  ํ”„๋กœ๊ทธ๋žจ์˜ ์•ˆ์ •์„ฑ๊ณผ ์‹ ๋ขฐ์„ฑ์ด ๋‚ฎ์•„์งˆ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.

๋‘๋ฒˆ์งธ๋กœ ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด์— ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์€ ๋ฌธ์ž์—ด ์กฐ์ž‘์— ์ฝ”๋“œ๊ฐ€ ๋” ๋ณต์žกํ•ด์ง€๊ณ  ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค. ๋‹ค์–‘ํ•œ ์กฐ๊ฑด๋“ค์„ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ต๊ณ  ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

๐ŸงฌQuery DSL

http://querydsl.com/static/querydsl/5.0.0/reference/html_single/#d0e97

ํƒ„์ƒํ•œ ๋ฐฐ๊ฒฝ์„ ์‚ดํŽด๋ณด๋ฉด HQL(Hibernate Query Language) ์ฟผ๋ฆฌ๋ฅผ ์œ ํ˜• ์•ˆ์ „ํ•œ ๋ฐฉ์‹์œผ๋กœ ์œ ์ง€ํ•ด์•ผ ํ•  ํ•„์š”์„ฑ์—์„œ ํƒ„์ƒ ํ–ˆ์œผ๋ฉฐ ๋ฌธ์ž์—ด์„ String ์—ฐ๊ฒฐ์ด ํ•„์š”ํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ์–ด๋ ต๊ฒŒ ๋œ๋‹ค.

์š”์•ฝํ•˜๋ฉด Query DSL์€ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ  ํŠนํžˆ ์ปดํŒŒ์ผ ์‹œ์ ์— ํƒ€์ž…์„ ์ฒดํฌํ•˜์—ฌ ํƒ€์ž…์— ์•ˆ์ •์„ฑ์„ ๋ณด์žฅํ•˜๊ณ  ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์—ฌ์ค€๋‹ค.
๋™์  ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ณ  ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์–ด ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•˜๋ฉฐ ๋ณต์žกํ•œ ์ฟผ๋ฆฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ฒ˜์Œ์— ํ•„์š”์„ฑ์ด ๋Š๋ผ์ง€ ๋ชป ํ–ˆ๋‹ค. ๋™์ ์œผ๋กœ ์—ฌ๋Ÿฌ ์ฟผ๋ฆฌ๋ฅผ ์กฐํšŒํ•œ๋‹ค? ๊ทธ๋ƒฅ JPA๋กœ ์ง์ ‘ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์กฐ์ธ ํ•˜๋ฉด ๋  ๊ฒƒ๊ฐ™์€๋ฐ? ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์ง€๋งŒ ์ ์  ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๋ฉฐ์„œ ๊ฒ€์ƒ‰์— ๋Œ€ํ•ด์„œ ์—ฌ๋Ÿฌ ํ•„ํ„ฐ๋‚˜ ๊ฒ€์ƒ‰ ์กฐ๊ฑด๋“ค์ด ๋งŽ์•„์ ธ์„œ ๊ฐœ๋ฐœ์˜ ์†๋„๊ฐ€ ๋Š๋ ค์ง€๊ฒŒ ๋˜์—ˆ๊ณ  ์–ด๋–ค ์กฐ๊ฑด๋“ค์„ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‚ฌ์ดํŠธ์—์„œ ์˜๊ฐ์„ ์–ป์–ด ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

query string์— ๋ธŒ๋žœ๋“œ์— ๋ฌด์‹ ์‚ฌ ์Šคํƒ ๋‹ค๋“œ ๊ธˆ์•ก์„ 30000~50000์› ์‚ฌ์ด๋กœ ๊ฒ€์ƒ‰์„ ํ–ˆ์„๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ url์„ ๋ณด์—ฌ์ค€๋‹ค.


์ถœ์ฒ˜ ๋ฌด์‹ ์‚ฌ


์ถœ์ฒ˜ ๋‹ค๋‚˜์™€

๐ŸŒปQuery DSL ์ ์šฉ

๐ŸŒฑQuery DSL ์˜์กด์„ฑ ์ถ”๊ฐ€

Query DSL์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

# build.gradle
// querydsl
	implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
    annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
    annotationProcessor "jakarta.annotation:jakarta.annotation-api"
    annotationProcessor "jakarta.persistence:jakarta.persistence-api"

๐ŸŒฑQuery DSL ์„ค์ •

Query DSL์˜ ์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค. ์—ฌ๊ธฐ์—์„œ Qclass ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๋Š”๋ฐ ํ•ด๋‹น ํŒŒ์ผ์˜ ์ƒ์„ฑ ์œ„์น˜ ๋ฐ ์ž‘์—…๋“ฑ์„ ์„ค์ •ํ•œ๋‹ค.

Qclass

QClass๋Š” Querydsl์—์„œ ์ƒ์„ฑ๋˜๋Š” ํด๋ž˜์Šค๋กœ, JPA ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์ฟผ๋ฆฌ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
์ด ํด๋ž˜์Šค๋“ค์€ ์ปดํŒŒ์ผ ์‹œ์ ์— Querydsl ์–ด๋…ธํ…Œ์ด์…˜ ํ”„๋กœ์„ธ์„œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ๋‹ค.
์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ์–ด์„œ, ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํƒ€์ž… ์•ˆ์ „ํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋ฉฐ ๋ฌธ์ž์—ด ๋Œ€์‹  ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ
์ปดํŒŒ์ผ ์‹œ์ ์— ์˜ค๋ฅ˜๋ฅผ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๋‹ค.

# build.gradle
// querydsl ์„ค์ •
def generated = 'src/main/generated' // Querydsl์ด ์ƒ์„ฑํ•œ QClass ํŒŒ์ผ์„ ์ €์žฅํ•  ์œ„์น˜๋ฅผ ์„ค์ •

// querydsl QClass ํŒŒ์ผ ์ƒ์„ฑ ์œ„์น˜๋ฅผ ์ง€์ •
tasks.withType(JavaCompile) {
    options.getGeneratedSourceOutputDirectory().set(file(generated))
}

// java source set ์— querydsl QClass ์œ„์น˜ ์ถ”๊ฐ€ Qclass ํŒŒ์ผ์ด ์ปดํŒŒ์ด๋œ ์ž๋ฐ” ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ๋นŒ๋“œ๋˜๋„๋ก ์„ค์ •
sourceSets {
    main.java.srcDirs += [generated]
}

// gradle clean ์‹œ์— QClass ๋””๋ ‰ํ† ๋ฆฌ ์‚ญ์ œ
clean {
    delete file(generated)
}

// Querydsl์ด ์ƒ์„ฑํ•œ QClass ํŒŒ์ผ์ด JAR ํŒŒ์ผ์— ํฌํ•จ๋˜์ง€ ์•Š๋„๋ก ์„ค์ •
jar {
    enabled = false
}

๐ŸŒฑEntity ์ถ”๊ฐ€

@Entity
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@SQLDelete(sql = "UPDATE partys SET deleted_at=CURRENT_TIMESTAMP where id=?")
@Where(clause = "deleted_at IS NULL")
@Table(name = "partys")
public class Party extends Timestamped {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String title;

    @Column(nullable = false)
    private String contents;

    @Column(nullable = false)
    private Boolean status;

    @Column(nullable = false)
    private int view;

    @Column(nullable = false)
    private LocalDateTime deadline;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "game_id")
    private Game game;
 }

๐ŸŒฑGradble build

Gradle ์ฐฝ์„ ๋ณด๋ฉด build ํด๋”์— build๋ผ๋Š” ์•„์ด์ฝ˜์„ ํด๋ฆญ์„ํ•œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด gradle ํŒŒ์ผ์— query dsl์— ์„ค์ •๋œ 'src/main/generated'์— QClass ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค.

๐ŸŒฑRepository ์ž‘์—…

QeuryDSL์„ ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•ด 4๊ฐ€์ง€ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

  • PartyRepository : PartyRepositoryCustom๋ฅผ ์ƒ์† ๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ๊ธฐ๋ณธ์ ์ธ JPA ์ธํ„ฐํŽ˜์ด์Šค

  • PartyRepositoryCustom : Querydsl์˜ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•ด ์ธํ„ฐํŽ˜์ด์Šค ์„ ์–ธ

  • PartyRepositoryCustomImpl : PartyRepositoryCustom ๊ตฌํ˜„์ฒด

  • PartySearchCondition : Querydsl์˜ ๋™์  ์ฟผ๋ฆฌ๋ฅผ ์œ„ํ•œ ์กฐ๊ฑด๋“ค

๋™์  ์ฟผ๋ฆฌ ์ ์šฉ ๋ฐฉ๋ฒ•

BooleanBuilder

Where ๋‹ค์ค‘ ํŒŒ๋ผ๋ฏธํ„ฐ

  • where ์กฐ๊ฑด์— null ๊ฐ’์€ ๋ฌด์‹œ๋˜๋ฉฐ ์ฟผ๋ฆฌ ์ž์ฒด์˜ ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง€๊ณ  ๋‹ค๋ฅธ ์ฟผ๋ฆฌ์—์„œ๋„ ์žฌ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.


BooleanExpression์œผ๋กœ SearchCondition์— ๊ฒ€์ƒ‰ ์กฐ๊ฑด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค. null์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด where ์ ˆ์—์„œ ํ•ด๋‹น ์กฐ๊ฑด์„ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

๐ŸŒฑ๊ฒฐ๊ณผ๋ฅผ DTO๋กœ ๋ฐ˜ํ™˜ ๋ฐฉ๋ฒ•

๋‚˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ์—”ํ‹ฐํ‹ฐ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ• ์ง€ ์•„๋‹ˆ๋ฉด domain์œผ๋กœ ๋ฐ˜ํ™˜ํ• ์ง€๋Š” ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ณ„์†ํ•ด์„œ ๊ณ ๋ฏผํ•ด์•ผํ•  ์‚ฌํ•ญ์ด๋‹ค.

๐ŸŒฑQuerydsl ํŽ˜์ด์ง• ์—ฐ๋™

Querydsl๋„ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ๋ฐ Pageable์„ ์ธ์ž๋กœ ๋ฐ›๋Š” ํ˜•ํƒœ๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค.

offset์„ ํ†ตํ•ด Pageable์˜ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ๋ฅผ ์„ค์ •ํ•˜๊ณ  limit์— ํŽ˜์ด์ง€ ์‚ฌ์ด์ฆˆ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

๐ŸŒฑQuerydsl ์ •๋ ฌ

์ •๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ๋ณดํ†ต orderby์— ์‚ฝ์ž…ํ•˜๋ฉด ๋˜์ง€๋งŒ ์ด๊ฒƒ ์—ญ์‹œ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•˜๋‹ค
์ผ๋ฐ˜ ์กฐ๊ฑด ๋ช…์ด ์•„๋‹Œ OrderSpecifierํ˜•์ด ํ•„์š”ํ•˜๋‹ค.

OrderSpecifier

QueryDSL์—์„œ ์ •๋ ฌ ์กฐ๊ฑด์„ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค์ด๋ฉฐ ORDER BY์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. ๋จผ์ € Pageable ๊ฐ์ฒด์—์„œ ์ •๋ ฌ ์กฐ๊ฑด์„ ๊ฐ€์ ธ์˜จ๋‹ค.
  2. ์ •๋ ฌ ์กฐ๊ฑด์ด ์กด์žฌํ•˜๋ฉด created(์ƒ์„ฑ์ผ), view(์กฐํšŒ์ˆ˜)๋ผ๋Š” ์ •๋ ฌ ์กฐ๊ฑด์ด ์กด์žฌํ•˜๋ฉด ํ•ด๋‹น ์กฐ๊ฑด์„ ๋ฆฌ์ŠคํŠธ์— ์‚ฝ์ž…ํ•œ๋‹ค.(์ •๋ ฌ ์กฐ๊ฑด ์ถ”๊ฐ€ ๊ฐ€๋Šฅ)
  3. ๋งŒ์•ฝ ์ •๋ ฌ ์กฐ๊ฑด์ด ์กด์žฌ ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‚ด๋ฆผ ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐ŸŒป๊ฒฐ๊ณผ

ํŒŒํ‹ฐ์—์„œ ๊ฒŒ์ž„์ด ๋ฆฌ๊ทธ์˜ค๋ธŒ๋ ˆ์ „๋“œ์ด๋ฉด์„œ ์ œ๋ชฉ์ด ๋žญํฌ๋ผ๋Š” ๊ธ€์ž๊ฐ€ ๋“ค์–ด๊ฐ€๊ณ  ์กฐํšŒ์ˆ˜๊ฐ€ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ(์ •๋ ฌ์‹œ ์ฝค๋งˆ๋ฅผ ๋„ฃ์–ด์•ผํ•œ๋‹ค. ,asc ,desc)

๐Ÿšจ๋ฌธ์ œ์  ๋ฐ ๊ฐœ์„  ์‚ฌํ•ญ

QClass ๋ถ€์žฌ

QClass๋ฅผ importํ• ๋•Œ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด๋•Œ build๋ฅผ Intellij์—์„œ gradle๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

OrderSpecifier null

์กฐํšŒ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆด๋•Œ ๋งŒ์•ฝ ์ •๋ ฌ ์ˆœ์„œ ๋ฐ˜ํ™˜๊ฐ’์„ null๋กœ ์ฃผ์—ˆ์„๋•Œ
QueryDSL์˜ OrderBy๋Š” null์„ ์ง€์›ํ•˜์ง€ ์•Š์•„ NullExpression.DEFAULT๋ฅผ ์‚ฌ์šฉ ๋ฐ ๊ถŒ์žฅํ•œ๋‹ค.


fetch join

ํŒŒํ‹ฐ์˜ ๊ฒŒ์ž„, ์ž‘์„ฑ์ž๋ฅผ ์กฐ์ธ ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๋‚ ๋ฆด๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฟผ๋ฆฌ๋ฅผ 3๊ฐœ๋ฅผ ๋”ฐ๋กœ ๋‚ ๋ฆฌ๋ฉด์„œ ์ˆ˜ํ–‰ํ•œ๋‹ค.

fetch join์„ ์ด์šฉํ•ด์„œ ์ฟผ๋ฆฌ๋ฅผ ํ•œ๋ฒˆ์— ๋ณด๋‚ด ์ฟผ๋ฆฌ ๊ฐฏ์ˆ˜๋ฅผ ์ค„์˜€๋‹ค.


๐ŸŽ‰Done

์ฐธ๊ณ ์ž๋ฃŒ
์ธํ”„๋Ÿฐ ์‹ค์ „ Querydsl
https://thalals.tistory.com/400

0๊ฐœ์˜ ๋Œ“๊ธ€