【发布时间】:2017-08-06 08:58:53
【问题描述】:
在 Spring Boot 应用程序中,我有一个在 postgresql 服务器上执行的 SQL 查询,如下所示:
@Query(value = "select count(*) from servers where brand= coalesce(?1, brand) " +
"and flavour= coalesce(?2, flavour) ; ",
nativeQuery = true)
Integer icecreamStockCount(String country, String category);
然而,
执行该方法时出现以下错误:
ERROR: COALESCE types bytea and character varying in PostgreSQL
如何将 String value = null 传递给查询?
**注意:** 我发现我的问题与 JPA Query to handle NULL parameter value 不同
【问题讨论】:
-
我不知道这是否可能。如果某些输入为空,我会尝试创建单独的查询。
-
FWIW 没有 JPA
@Query注释。那是春天。 -
你可以把coalesce改成
case when then else end,比如"select count(*) from servers where brand = (case when ?1 = 'undefined' then brand else ?1::bytea) "
标签: postgresql hibernate jpa spring-boot