Verifying QueryDSL Configuration for QFile in Spring Boot 3.X.X

#Foreword

Below is a very simple verification process for QFile and QueryDSL configuration.

Frankly, now I see that the process is very straightforward, but the transition to Spring Boot 3.X.X and other factors caused some issues. So, to remind myself of this process, I'm leaving my trace. Thanks to the initial QueryDSL configuration process, I can understand the flow.

build.gradle : spring 3.X + querydsl 5.0.0

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 logging parameter with Spring Boot 3.X
    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)
}

1.Create Entity Class

2.Create Controller

3.Localhost:8080/evening

4.clean -> build

5.Check if QFile is successfully generated.

/src/main/generated,

If you see QFile generated, you can consider it as a successful signal from querydsl.

(QFile can be generated based on your Entity class)

6.Write the TestCode to verify Getter and JPA

package study.querydsl.entity;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
@SpringBootTest
@Transactional
class EveningTest {

    // Inject EntityManager for persistence operations
    @PersistenceContext
    EntityManager em;

    // Starts a Spring application context and runs the test within a transaction
    @Test
    void contextLoads() {
        // Create and persist an Evening entity
        Evening evening = new Evening();
        em.persist(evening);

        // Create a JPAQueryFactory for building and executing QueryDSL queries
        JPAQueryFactory query = new JPAQueryFactory(em);
        QEvening qEvening = QEvening.evening;

        // Use QueryDSL to fetch the persisted Evening entity from the database
        Evening result = query
            .selectFrom(qEvening)
            .fetchOne();

        // Assert that the fetched entity is the same as the persisted entity
        Assertions.assertThat(result).isEqualTo(evening);

        // Check if the getter method for the id field works correctly, ensuring that Lombok-generated methods are functioning as expected
        Assertions.assertThat(result.getId()).isEqualTo(evening.getId());
    }
}

+Transactional annotation

@Transactional This annotation ensures that each test method is run only within a transaction, which will be rolled back after the method executed, preventing test data from polluting the database.

Simply put, Test data can affect the existing database without @Transactional!

Tips for QFile

: QFile is automatically generated during compilation, you can add it to .gitignore file.

Normally people set the QFile that can be included in gradle build folder, but I didn't set it in that way, I generated it /src/main/generated, If I want to update this, I'd better to add this QFile into .gitignore.

Like below.