mysqlspringspring-boothibernatequerydsl

Error while using Enum in QueryDSL after version upgrade to 5.0.0


I am using querydsl with mysql in my spring boot project. Previously the followring mentioned code was working fine but after the project upgrade i.e. spring boot to 3 and querydsl to 5.0.0, I am getting the error.

Code:

public List<ProductMiniResource> getAllProductsByProductCode(String[] productCodes) {
        return from(product)
                .where(product.code.in(productCodes)
                        .and(product.status.eq(ProductStatus.ACTIVE)))
                .select(new QProductMiniResource(product.id,
                        product.code,
                        product.name,
                        product.logoPath,
                        product.stockStatus.eq(StockStatus.AVAILABLE),
                        product.productRedirectUrl,
                        product.displayName,
                        product.inquiryType,
                        product.allowReminder,
                        product.allowSchedulePayment,
                        product.allowSavePayment))
                .fetch();
       }

Error is shown at line product.stockStatus.eq(StockStatus.AVAILABLE). If I remove this part then it works fine. There is no any compile time error.

Error at p1_0.stockStatus=cast(? as smallint)

16:43:30.803 [http-nio-2280-exec-1] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1064, SQLState: 42000
16:43:30.804 [http-nio-2280-exec-1] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'smallint),p1_0.product_redirect_url,p1_0.display_name,p1_0.inquiry_type,p1_0.all' at line 1
16:43:31.040 [http-nio-2280-exec-1] ERROR com.app.advice.GlobalExceptionHandler - Error JDBC exception executing SQL [select p1_0.id,p1_0.code,p1_0.name,p1_0.logo_path,p1_0.stockStatus=cast(? as smallint),p1_0.product_redirect_url,p1_0.display_name,p1_0.inquiry_type,p1_0.allow_reminder,p1_0.allow_schedule_payment,p1_0.allow_save_payment from products p1_0 where p1_0.code=? and p1_0.status=?]; SQL [n/a]

Solution

  • Your proposed solution uses stringValue() and name() methods to compare the string representation of the stockStatus enum with the StockStatus.AVAILABLE enum name.

    Here is a possible solution:

    public List<ProductMiniResource> getAllProductsByProductCode(String[] productCodes) {
        return from(product)
                .where(product.code.in(productCodes)
                        .and(product.status.eq(ProductStatus.ACTIVE)))
                .select(new QProductMiniResource(product.id,
                        product.code,
                        product.name,
                        product.logoPath,
                        product.stockStatus.stringValue().eq(StockStatus.AVAILABLE.name()),
                        product.productRedirectUrl,
                        product.displayName,
                        product.inquiryType,
                        product.allowReminder,
                        product.allowSchedulePayment,
                        product.allowSavePayment))
                .fetch();
       }