1

I have this code where I build transaction:

public BooleanBuilder findAllTransactionsByUserId(Long userId, DateTime startDate, DateTime endDate, 
      String firstName, String lastName, String email) {

    QTransaction transaction = QTransaction.transaction;
    BooleanBuilder where = getTransactionWhereClause(startDate, endDate, firstName, lastName, email);
    where.and(transaction.userId.eq(userId));

    return where;
  }

  public BooleanBuilder getTransactionWhereClause(DateTime startDate, DateTime endDate,
      String firstName, String lastName, String email) {

    BooleanBuilder where = new BooleanBuilder();
    QTransaction transaction = QTransaction.transaction;

    if (startDate != null && endDate != null) {
      where.and(transaction.transactionDate.between(startDate, endDate));
    }
    if (firstName != null) {
      where.and(transaction.firstName.eq(firstName));
    }
    if (lastName != null) {
      where.and(transaction.lastName.eq(lastName));
    }
    if (email != null) {
      where.and(transaction.email.eq(email));
    }
  
    return where;
  }

I'm missing the code for the class QTransaction and I can't figure out what might be the content of the Java class so that I can create it manually.

Do you have any idea how I can implement the missing code?

I tried this gradle configuration:

plugins {
    id 'org.springframework.boot' version '2.6.7'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'test'
version = '0.0.1'
sourceCompatibility = '17'

ext {
    set('springCloudVersion', "2021.0.2")
    queryDslVersion = '5.0.0'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.hibernate.validator:hibernate-validator'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'joda-time:joda-time:2.10.14'
    implementation 'org.springframework.boot:spring-boot-starter-hateoas:2.6.7'
    implementation 'org.postgresql:postgresql'
    implementation 'org.jadira.usertype:usertype.core:7.0.0.CR1'
    implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.2'
    implementation 'org.apache.commons:commons-lang3:3.12.0'
    implementation 'com.google.code.gson:gson:2.9.0'

    // QueryDSL
    implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
    annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
    testImplementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
    testAnnotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
    
    // Lombok
    compileOnly 'org.projectlombok:lombok:1.18.24'
    annotationProcessor 'org.projectlombok:lombok:1.18.24'
    testCompileOnly 'org.projectlombok:lombok:1.18.24'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
    // Swagger
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.8'

    implementation 'org.liquibase:liquibase-core'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

tasks.named('test') {
    useJUnitPlatform()
}

But JPA classes are not generated. I get exception:

import com.test.domain.QTransaction;
                       ^
  symbol:   class QTransaction
  location: package com.test.domain

Do you know what is the proper way to implement this?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • It's a JPA metamodel class, that's generated by JPA, not written by you. – Kayaman May 16 '22 at 10:09
  • Any idea how I can configure the project to generate this class? – Peter Penzov May 16 '22 at 10:31
  • You don't have any ideas? Even though I gave you keywords to Google for? I'll give you a link in one week if you haven't managed to find any information yourself before that (remember to ping me in one week if necessary). – Kayaman May 16 '22 at 10:44
  • My bad, it's actually [QueryDSL](https://stackoverflow.com/q/16662089/2541560), not JPA metamodel (which uses `Student_.class` naming). – Kayaman May 17 '22 at 18:35

1 Answers1

1

The Q classes are typically generated by QueryDSL and the QueryDSL annotation processor tool.

I am not as familiar with Gradle as I am with Maven, but the Gradle configuration looks fine to me. Just in case, see this or this other related SO questions.

When using with JPA, as a first step and as you guessed, you must create a class that represent your table, you must create an @Entity with the required information. In your case, it would look like similar to this code:

@Entity
public class Transaction {

    @Id
    @GeneratedValue
    private Long id;

    private Long userId;

    private String firstName;

    private String lastName;

    private String email;

    private DateTime transactionDate;

    // getters and setters

}

Please, note that we annotated the class with @Entity.

After compiling your classes, you will see that Gradle should have generated a class with the name QTransaction.

Among other information, it will generate a public static final instance of the QTransaction class:

public static final QTransaction transaction = new QTransaction("transaction");

This class is the one you will typically use when constructing your queries, as in your example.

This article, although Maven focused, could be of help.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
  • Thank you! As always great answer. I found one more issue: https://stackoverflow.com/questions/72337736/migrate-resourcesupport-to-representationmodel Can you give some advise how to fix it? – Peter Penzov May 22 '22 at 15:09
  • You are welcome Peter. I am glad to hear that the answer was helpful. Yes, of course, I posted a new answer for your new question. I haven't tested it, but I hope it be of help. – jccampanero May 22 '22 at 15:23