我在我的 Spring Boot 项目中使用 querydsl 和 mysql。之前提到的以下代码工作正常,但在项目升级后,即 spring boot 到 3 和 querydsl 到 5.0.0,我收到错误。
代码:
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();
}
错误显示在第
product.stockStatus.eq(StockStatus.AVAILABLE)
行。如果我删除这部分,那么它就可以正常工作。没有任何编译时错误。
错误位于
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]
您提出的解决方案使用 stringValue() 和 name() 方法将 stockStatus 枚举的字符串表示形式与 StockStatus.AVAILABLE 枚举名称进行比较。
这是一个可能的解决方案:
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();
}