๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Spring

[Querydsl] unable to load class com.mysema.codegen.model.type'. error

by dkswnkk 2022. 7. 31.

๋ฌธ์ œ

์—๋Ÿฌ ๋‚ด์šฉ

ํ˜„์žฌ ํ”„๋กœ์ ํŠธ๋Š” spring-boot-2.7.2๋ฅผ ์‚ฌ์šฉ ์ค‘์ด๋ฉฐ, Querydsl์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด build.gradle์— ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ณผ์ •์—์„œ ์œ„์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

plugins {
    id 'org.springframework.boot' version '2.7.2'
    id 'io.spring.dependency-management' version '1.0.12.RELEASE'
    // querydsl ์ถ”๊ฐ€
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
    id 'java'
}

group = 'study'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {

    // querydsl ์ถ”๊ฐ€
    implementation 'com.querydsl:querydsl-jpa'

    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'com.h2database:h2'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

// ------------querydsl ์ถ”๊ฐ€ ์‹œ์ž‘ ------------
def querydslDir = "$buildDir/generated/querydsl"
querydsl {
    jpa = true
    querydslSourcesDir = querydslDir
}
sourceSets {
    main.java.srcDir querydslDir
}
configurations {
    querydsl.extendsFrom compileClasspath
}
compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}
// ------------querydsl ์ถ”๊ฐ€ ๋------------

Querydsl์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์„ค์ •ํ•œ build.gradle ์ฝ”๋“œ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํ•ด๊ฒฐ

// ------------ ์—ฌ๊ธฐ ์ถ”๊ฐ€ ------------
buildscript {
    ext {
        queryDslVersion = "5.0.0"
    }
}
// ------------------------


plugins {
    id 'org.springframework.boot' version '2.7.2'
    id 'io.spring.dependency-management' version '1.0.12.RELEASE'
    //---------------์ถ”๊ฐ€---------------------
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
    //---------------------------------------
    id 'java'
}

group = 'study'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    
    // ------------ ์—ฌ๊ธฐ ๋ณ€๊ฒฝ ------------
    implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
    implementation "com.querydsl:querydsl-apt:${queryDslVersion}"
    // ------------------------
    
    runtimeOnly 'com.h2database:h2'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}
def querydslDir = "$buildDir/generated/querydsl"

querydsl {
    jpa = true
    querydslSourcesDir = querydslDir
}
sourceSets {
    main.java.srcDir querydslDir
}
compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

// ------------ ์—ฌ๊ธฐ ๋ณ€๊ฒฝ ------------
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    querydsl.extendsFrom compileClasspath
}
// ------------------------

build.gradle์„ ์œ„์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•ด ์ค๋‹ˆ๋‹ค. ํŠนํžˆ dependency ๋ถ€๋ถ„์— querydsl-jpa , querydsl-apt๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฒ„์ „์„ ๋ช…์‹œํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์œ„ ์ฝ”๋“œ์—์„œ๋Š”  dependency querydsl-jpa , querydsl-apt์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ตœ ์ƒ๋‹จ์— ๋ฒ„์ „์„ ๋ช…์‹œํ–ˆ์Šต๋‹ˆ๋‹ค.)

ํ™•์ธ

์œ„ ์ด๋ฏธ์ง€ ์ฒ˜๋Ÿผ inteliJ์šฐ์ธก์˜ Gradle -> querydsl -> other -> compileQuerydsl์„ ๋”๋ธ” ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค๋ฉด ์„ฑ๊ณต์ž…๋‹ˆ๋‹ค!

์›์ธ

Srping Boot 2.6 ๋ฒ„์ „ ์ด์ƒ๋ถ€ํ„ฐ๋Š” Querydsl 5.0์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ถ€๋ถ„์„ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  1. build.gradle ์„ค์ • ๋ณ€๊ฒฝ (ํ•„์ˆ˜)
  2. PageableExecutionUtils Deprecated(ํ–ฅํ›„ ๋ฏธ์ง€์›) ํŒจํ‚ค์ง€ ๋ณ€๊ฒฝ (์‚ฌ์šฉํ•œ๋‹ค๋ฉด)
  3. Querydsl fetchResults() , fetchCount() Deprecated(ํ–ฅํ›„ ๋ฏธ์ง€์›) (์‚ฌ์šฉํ•œ๋‹ค๋ฉด)

 

PageableExecutionUtils ํŒจํ‚ค์ง€ ๋ณ€๊ฒฝ

PageableExecutionUtils ํด๋ž˜์Šค ์‚ฌ์šฉ ํŒจํ‚ค์ง€ ๋ณ€๊ฒฝ
PageableExecutionUtils ํด๋ž˜์Šค ์‚ฌ์šฉ ํŒจํ‚ค์ง€ ์œ„์น˜๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด ์œ„์น˜๋ฅผ ์‹ ๊ทœ ์œ„์น˜๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์‹œ๋ฉด ๋ฌธ์ œ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ธฐ์กด: org.springframework.data.repository.support.PageableExecutionUtils
์‹ ๊ทœ: org.springframework.data.support.PageableExecutionUtils

 

Querydsl fetchResults() , fetchCount() Deprecated(ํ–ฅํ›„ ๋ฏธ์ง€์›)

Querydsl์˜ fetchCount() , fetchResult()๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•œ select ์ฟผ๋ฆฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ count์šฉ ์ฟผ๋ฆฌ๋ฅผ ๋‚ด๋ถ€์—์„œ ๋งŒ๋“ค์–ด์„œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ ์ด ๊ธฐ๋Šฅ์€ select ๊ตฌ๋ฌธ์„ ๋‹จ์ˆœํžˆ count ์ฒ˜๋ฆฌํ•˜๋Š” ์šฉ๋„๋กœ ๋ฐ”๊พธ๋Š” ์ •๋„์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹จ์ˆœํ•œ ์ฟผ๋ฆฌ์—์„œ๋Š” ์ž˜ ๋™์ž‘ํ•˜์ง€๋งŒ, ๋ณต์žกํ•œ ์ฟผ๋ฆฌ์—์„œ๋Š” ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์œผ๋ฉฐ Querydsl์€ ํ–ฅํ›„ fetchCount() , fetchResult()๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ count ์ฟผ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ„๋„๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

@Test
  public void count() {
      Long totalCount = queryFactory
              //.select(Wildcard.count) //select count(*)
              .select(member.count()) //select count(member.id)
              .from(member)
              .fetchOne();
      System.out.println("totalCount = " + totalCount);
}

 

 

์ฐธ๊ณ 

https://www.inflearn.com/course/Querydsl-%EC%8B%A4%EC%A0%84

๋Œ“๊ธ€