I can't create Qfile on Spring Boot 3.xx With querydsl 5.0.0 (querydsl-jpa)
#Foreword
After releasing Spring Boot 3.X.X, there're several changes on querydsl configuration.
javax -> jakarta
querydsl plugin usages x
I've tried to googled it, I couldn't find out up-to-date version.
When adding querydsl plugin, I could see compileQuerydsl on Gradle, but If i try to use it, it says unable to load class...~~~
#Qfile - querydsl
QueryDSL is type-safe query, which is a Query Domain Specific Language. When using queryDSL, We need to verify it with the Qfile created. If you see Qfile from a specific entity class, it means Querydsl is applied well to your application.
Initially, I thought if I add queryDSL dependencies, the set-up is done. But, As I heard, I need to ensure that Qfile is generated well, which is the used instance in QueryDsl queries.
(Qfile is created by tracing generated entity class automatically)
+ After releasing Spring Boot 3.X.X, we need to consider some set up, I will add those set-up.
#Verifying queryDsl Configuration
1.Add dependencies
- Ensure QueryDSL dependencies are added to
build.gradle
2.Q-file Generation
Verify that Q-file are generated in appropriate directory :
src/main/generated
3.Qfile Usage Verification
Confirm that Q-file is generated based on your entity class.
ex)
Hello
Entity class -> should beQHello
dependencies
Spring Web, Jpa, H2, Lombok => and then I added Querydsl
#Gradle Spring 2.x : deprecated
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
#querydsl plugin
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
id 'java'
}
group = 'study'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
#querydsl dependencies
implementation 'com.querydsl:querydsl-jpa'
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()
}
#Add querydsl - start point
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 - end point
#Gradle Spring 3.x Error
: Spring Boot 3.2.x With QueryDSL , there's no 'build' directory.
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'study'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
#test Lombok
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
#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"
}
tasks.named('test') {
useJUnitPlatform()
}
# delete generated Qfile when gradle 'clean'
clean {
delete file('src/main/generated')
}
After checking Test run / Main application run, I added 'Hello' entity class, QHello was supposed to be generated with this. (It turned out I missed several processes.)
add querydsl plugin
into build.gradle, and then clean and click gradle (elephant-shape icon)
compileQuerydsl is generated.
But, If I Click compileQuerydsl
Unable to load class ''.
This is an unexpected error. Please file a bug containing the idea.log file.
#Set appropriate directory for Q-file
/src/generated/main
#add dependencies : def ~~ 'src/main/generated'
clean -> build or compileJava
Finally, generated QFile : QHello in this case from Hello Entity.
(Q-File generate QClass based on specific entity.)
src/main/generated/groupname.querydsl.entity -> QHello
#Reference Point for Q-File
Qfile is automatically generated on compilation, so it's better not to add in Git, which means we can add this Qfile into .gitignore
#Querydsl Working Configuration
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'study'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
// Query Parameter Logging:Spring Boot3.0 + gavlyukovskiy1.9.0(external library)
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
//test Lombok
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
//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"
}
tasks.named('test') {
useJUnitPlatform()
}
# ------------------ added this appropriate file directory for Q-files generation
def querydslSrcDir = 'src/main/generated'
clean {
delete file(querydslSrcDir)
}
tasks.withType(JavaCompile) {
options.generatedSourceOutputDirectory = file(querydslSrcDir)
}
#In Conclusion,
I've spent almost a day trying to resolve this. Frankly, I've tried this QueryDSL before,,,,, but I couldn't generate Q-Files for some reason.
Initially, I wasn't good at configuring project set-up, I couldn't think of a solution for this, rather, I am embarrassed to say this.. I put this aside.
Anyway, This means a lot, whenever a new version comes out, we need to consider compatibility for specific versions, that's why we need a reference point to use.
Spring Boot 2.x.x is deprecated, so ChatGPT and Google's information can be wrong. When you need to try to look into it, you need to check its version or posted date.
Configuring Initial set up for the new is never easy for you,,,,,,,,,,,,I'd better leave my trace to leave the processes for this.
+Configuration Tips for Spring Boot 3
Package name
should be : javax => jakarta
Build, Execution, Deployment Gradle
: After releasing Spring Boot 3.2x, It should be JDK17 and Gradle(Instead of IntelliJ)
#Just Reference for myself 2024.07.09
#'generated [test] file' is a serious error?
: To say the conclusion first, I've tried to separate those 'generated' files, but I failed to use, Whenever I try to use destinationDir = file(querydslMainSrcDir) and destinationDir = file(querydslTestSrcDir)
, I found it deprecated..... and I heard it's not serious problem or error, so I can make progress with generated [test], actually file name is generated, [test] part is just seen on IntelliJ like below.
Anyway, I will make progress with my original set-up that I posted above, but I will leave this trace for my future possibility..... not sure, but I would try to resolve this in some cases.
In some cases, I added this configuration too.
#Seperating the main generated and test (Gradle)
After addting this below.
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.4'
}
//~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~
//~~~~~Another gradle setup.
dependencies {
~~~~~~~~~~~~
}
tasks.named('test') {
useJUnitPlatform()
}
-----------------------------------------
def querydslMainSrcDir = 'src/main/generated/querydsl'
def querydslTestSrcDir = 'src/test/generated/querydsl'
clean {
delete file(querydslMainSrcDir)
delete file(querydslTestSrcDir)
}
task generateMainQuerydslSources(type: JavaCompile) {
source = sourceSets.main.java
classpath = sourceSets.main.compileClasspath
destinationDir = file(querydslMainSrcDir)
options.generatedSourceOutputDirectory = file(querydslMainSrcDir)
}
task generateTestQuerydslSources(type: JavaCompile) {
source = sourceSets.test.java
classpath = sourceSets.test.compileClasspath
destinationDir = file(querydslTestSrcDir)
options.generatedSourceOutputDirectory = file(querydslTestSrcDir)
}
generateMainQuerydslSources.doFirst {
mkdir querydslMainSrcDir
}
generateTestQuerydslSources.doFirst {
mkdir querydslTestSrcDir
}
// Add the generated source directories to the source sets
sourceSets {
main {
java {
srcDirs += querydslMainSrcDir
}
}
test {
java {
srcDirs += querydslTestSrcDir
}
}
}
compileJava.dependsOn(generateMainQuerydslSources)
compileTestJava.dependsOn(generateTestQuerydslSources)
clean -> build, generated files are successfully separated.
and after compileJava
Same....