在springboot中使用查询时出现jdbcMapping错误的枚举值

Enum value giving jdbcMapping error while using query in springboot

提问人:dracile 提问时间:11/14/2023 最后编辑:dracile 更新时间:11/14/2023 访问量:60

问:

我的要求是按 patientId、patientType 和 DeletedAt、DeletedBy、DeletedByUserType 查找条目,这三个应该是 null

我使用了这个查询,但它无法映射 patientType,这是一个 ENUM

错误

"An error occurred during data insertion in An error occurred during data insertion in Cannot invoke \"org.hibernate.metamodel.mapping.JdbcMapping.getJdbcValueBinder()\" because \"jdbcMapping\" is null"

查询

 @Transactional
 @Query(value="SELECT * FROM tb_patient o WHERE o.patientId = :patientId AND o.patientType = :patientType AND o.deletedAt Is Null AND o.deletedBy Is Null AND o.deletedByUserType Is Null ", nativeQuery = true)

    List<PatientInsurance> findCustomQuery(@Param("patientId")String patientId, @Param("patientType")PatientTypeEnum patientType);
    

Enum 类

public enum PatientTypeEnum {
    OPG("OPG"),
    OTS("OTS");
    private final String code;

    PatientTypeEnum(String code) {
        this.code = code;
    }
    public String getCode() {
        return code;
    }
}

实体类

private class InsuranceEntity{
   @Enumerated(EnumType.STRING)
    @Column(name = "patientType", length = 3, nullable = false)
    private PatientTypeEnum patientType;
}
java spring-boot hibernate jpa 枚举

评论

0赞 dracile 11/14/2023
至少给出反对票的理由,或任何相关帖子
0赞 Jens 11/14/2023
不能在本机查询中使用枚举。尝试使用 jpql 或枚举的名称作为字符串。
0赞 dracile 11/14/2023
@Jens如何以及需要哪些更改,请您强调一下
0赞 Jens 11/14/2023
删除 native=true
1赞 Jens 11/14/2023
顺便说一句:我总是建议不要在春季环境中使用本机查询。

答:

2赞 JeslynTan 11/14/2023 #1

也许在@Query上试试这个

o.patientType=:#{#patientType?.code()}

而不是 o.patientType = :p atientType

参考:我可以在JpaRepository nativeQuery中使用枚举参数吗?