【问题标题】:COALESCE function in JPAJPA 中的 COALESCE 函数
【发布时间】: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]

Erro Run Project

我的代码:

@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


【解决方案1】:

我遇到了同样的问题:COALESCE 与 Postgress 一起工作,它使 H2 数据库崩溃了相同的异常,所以我创建了一个实现两个存储库接口(产品实体)并使用注释 @profile 的解决方法,然后在编译时将构建正确的查询. 首先,我创建了一个基本存储库接口,没有扩展任何其他接口:

public interface ProductRepositoryBase {
    
    Page<Product>      findCustomized(List<Category> categories, String name, Pageable page);
    Optional<Product>  findById(Long id);
    Product save(Product entity);
    void deleteById(Long id);
}

然后我创建了另外两个接口(每个配置文件一个),一个用于使用 postgres db (dev) 进行开发:

@Profile("dev")  
public interface ProductRepositoryDev extends ProductRepositoryBase,JpaRepository<Product, Long>  {
    
    @Query("SELECT DISTINCT obj FROM Product obj INNER JOIN obj.categories cats "
             + "WHERE (COALESCE(:categories,null) IS NULL OR cats IN :categories) AND "
             + "(LOWER(obj.name) LIKE LOWER(CONCAT('%',:name,'%' )))")
        Page<Product> findCustomized(List<Category> categories, String name, Pageable page);

}

另一个用于测试(使用 H2 数据库):

@Profile("test")
public interface ProductRepositoryTest extends ProductRepositoryBase,JpaRepository<Product, Long> {

    @Query("SELECT DISTINCT obj FROM Product obj INNER JOIN obj.categories cats "
             + "WHERE (:categories IS NULL OR cats IN :categories) AND "
             + "(LOWER(obj.name) LIKE LOWER(CONCAT('%',:name,'%' )))")
    Page<Product> findCustomized(List<Category> categories, String name, Pageable page);

}

请注意,我有问题的查询与 findCustomized 方法相关联,但是在基本接口中,您必须声明任何其他存储库,您在 Product 实体服务层中使用的方法,否则在运行代码时它将崩溃,因为这些函数未在中定义基础界面。

【讨论】:

    猜你喜欢
    • 2012-11-02
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多