使用@Query JPA批注将null参数传递给本机查询

问题描述 投票:4回答:2

在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不同

postgresql hibernate jpa spring-boot
2个回答
4
投票

你不需要合并,试试这个

@Query("select count(*) from servers where (brand = ?1 or ?1 is null)" +
        " and (flavour = ?2 or ?2 is null)")
Integer icecreamStockCount(String country, String category);

0
投票

在Hibernate 5.0.2之前不允许使用null参数。请参阅https://hibernate.atlassian.net/browse/HHH-9165以及对https://www.postgresql.org/message-id/[email protected]的回复

© www.soinside.com 2019 - 2024. All rights reserved.