0

只传递了一个@Param,它运行良好,如下所示,无论是名字还是姓氏。spring-boot 版本 2.2.0.RELEASE 正在使用。

 @Query(value = "select s from SalesTransaction s where (:firstName is null or LOWER(s.firstName) LIKE LOWER(concat('%',concat(:firstName,'%'))))")
List<SalesTransaction> findFromFirstName(@Param("firstName") String firstName);

但是如果通过两个 @Params 它会抛出以下异常。firstName 和 lastName 在实体定义中属于 TEXT 类型。如果 firstName 正在传递,则 lastName 从 Controller 设置为“null”,反之亦然。

@Query(value = "select s from SalesTransaction s where (:firstName is null or LOWER(s.firstName) LIKE LOWER(concat('%',concat(:firstName,'%')))) and (:lastName is null or LOWER(s.lastName) LIKE LOWER(concat('%',concat(:lastName,'%'))))")
List<SalesTransaction> findFromFirstName(@Param("firstName") String firstName,@Param("lastName") String lastName);

运行时异常:

[32m2020-05-11 21:09:17,237[0;39m [31mWARN [0;39m [[34mhttp-nio-8080-exec-5[0;39m] [33morg.hibernate.engine.jdbc.spi.SqlExceptionHelper[0;39m: SQL Error: 0, SQLState: 42883
[32m2020-05-11 21:09:17,237[0;39m [1;31mERROR[0;39m [[34mhttp-nio-8080-exec-5[0;39m] [33morg.hibernate.engine.jdbc.spi.SqlExceptionHelper[0;39m: ERROR: function lower(bytea) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.
  Position: 1324

一次只有一个输入 @Param 正在工作。请提出任何解决此问题的方法。

Hibernate 生成的 SQL 查询

salestrans0_.transaction_ref as transac28_1_ from sales_transaction.sales_transaction salestrans0_ where (? is null or lower(salestrans0_.first_name) like lower(('%'||(?||'%')))) and (? is null or lower(salestrans0_.last_name) like lower(('%'||(?||'%'))))
4

0 回答 0