【发布时间】:2023-02-03 05:04:39
【问题描述】:
我正在 MVC 层模式中开发应用程序。
在我注意到 GET 查询的部分代码中,来自 RecordRepository 使用 COALESCE 函数,它在控制台中返回以下错误,如下图所示。我正在配置此功能,因为在 Postgres 中它不仅有效(:min IS NULL)。
org.h2.jdbc.JdbcSQLNonTransientException:未知数据类型:“NULL,?”;语句: 选择 record0_.id 作为 id1_2_,record0_.age 作为 age2_2_,record0_.game_id 作为 game_id5_2_,record0_.moment 作为 moment3_2_,record0_.name 作为 name4_2_ from tb_record record0_ where (coalesce(?, null) is null or record0_.moment>=? ) and (coalesce(?, null) is null or record0_.moment<=?) order by record0_.moment desc limit ? [50004-214]
我的代码:
@Repository public interface RecordRepository extends JpaRepository<Record, Long>{ @Query("SELECT obj FROM Record obj WHERE " + "(COALESCE(:min, null) IS NULL OR obj.moment >= :min) AND " + "(COALESCE(:max, null) IS NULL OR obj.moment <= :max)") Page<Record> findByMoments(Instant min, Instant max, Pageable pageable); }
【问题讨论】:
-
根据COALESCE documentation,它返回第一个不为空的参数。您希望通过
COALESCE(:min, null)实现什么目标? -
测试如下,如果条件 (obj.monet >= :min) 没有 (min) 或 (max) 的值,我将确保该条件为真。查询,无合并 - (:min IS NULL OR obj.moment >= :min) 当我在没有合并功能的情况下使用它时,端点查询有效,但使用合并功能时,我无法执行查询。
-
当我在 H2 数据库上执行测试时显示此错误,在 postgres 中,我设法运行查询
-
coalesce()返回第一个非空值。coalesce(:min, null)没有意义,和写:min一样 -
如果您明确使用 H2,为什么要使用
postgresql标签?
标签: sql postgresql spring-data-jpa primavera