Spring Data Jpa 3.0.0 YesNoConverter 和 JpaRepository-query 错误

Guy*_*ood 5 hibernate spring-data-jpa

我正在使用 hibernate YesNoConverter 在数据库中将布尔值存储为“Y”/“N”。目前,我正在迁移到 Spring Boot 3.0(使用 spring-data-jpa:3.0.0),并且自定义 JpaRepository 查询“findByActiveFalse”崩溃。

List<ExampleEntity> findByActiveFalse();
Run Code Online (Sandbox Code Playgroud)

错误:

org.springframework.dao.DataIntegrityViolationException: JDBC exception executing SQL [select e1_0.id,e1_0.active from example_entity e1_0 where e1_0.active=0]; SQL [n/a]

Run Code Online (Sandbox Code Playgroud)

Hibernate YesNoConverter 不会被触发,因此有一个零而不是“N”。(在升级到 Spring Boot 3.0 之前,我有一个自己的 Converter 实现,它也有同样的问题)

使用“findByActive(false)”,查询可以正常工作。

这是我的资料来源:

JpaRepository:https://github.com/GuybrushDevwood/boolean-converter-demo/blob/main/src/main/java/com/example/booleanconverterdemo/example/ExampleEntityRepo.java

实体: https: //github.com/GuybrushDevwood/boolean-converter-demo/blob/main/src/main/java/com/example/booleanconverterdemo/example/ExampleEntity.java

测试用例: https: //github.com/GuybrushDevwood/boolean-converter-demo/blob/main/src/test/java/com/example/booleanconverterdemo/example/ExampleEntityRepoTest.java

我需要配置什么才能让它像以前一样工作吗?

Rob*_*hof 0

由于您不想重构代码,因此这是一个很好的临时解决方案。

public interface ExampleRepository extends JpaRepository<ExampleEntity, String> {
    List<ExampleEntity> findByActive(boolean active);

    default List<ExampleEntity> findByActiveFalse(){
        return findByActive(false);
    }
}
Run Code Online (Sandbox Code Playgroud)

选择:

@Query("select e FROM exampleentity where e.active= true");
Run Code Online (Sandbox Code Playgroud)

使用标准构建器:

builder.equals(root.get("repeat), true);
Run Code Online (Sandbox Code Playgroud)