在 springboot 中使用查询时,枚举值给出 jdbcMapping 错误

问题描述 投票:0回答:1

我的要求是通过病人ID、病人类型查找条目,并且DeletedAt、DeletedBy、DeletedByUserType这三个应该为空

我使用了这个查询,但它无法映射作为 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_insurance_map 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);
    

JPA 日志

QueryPlan#resolveNativeQueryParameters(SELECT * FROM tb_patient_insurance_map o WHERE o.patientId = :patientId AND o.patientType = :patientType AND o.deletedAt Is Null AND o.deletedBy Is Null AND o.deletedByUserType Is Null )
2023-11-14T11:18:54.037+05:30 DEBUG 27232 --- [nio-8080-exec-1] org.hibernate.orm.query.plan.cache       : Creating and caching NativeQuery ParameterInterpretation - ParameterInterpretationImpl (SELECT * FROM tb_patient_insurance_map o WHERE o.patientId = ? AND o.patientType = ? AND o.deletedAt Is Null AND o.deletedBy Is Null AND o.deletedByUserType Is Null ) : {
    ,
    
}
SELECT * FROM tb_patient_insurance_map o WHERE o.patientId = ? AND o.patientType = ? AND o.deletedAt Is Null AND o.deletedBy Is Null AND o.deletedByUserType Is Null 
2023-11-14T11:18:54.039+05:30 TRACE 27232 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind              : binding parameter [1] as [VARCHAR] - [4b796737-1b67-411b-b281-10b92812b80a]
2023-11-14T11:18:54.048+05:30 ERROR 27232 --- [nio-8080-exec-1] c.d.p.s.impl.PatientDetailServiceImpl    : An unexpected error occurred
2023-11-14T11:18:54.048+05:30 ERROR 27232 --- [nio-8080-exec-1] c.d.p.s.impl.PatientDetailServiceImpl    : An unexpected error occurred

枚举类

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;
}

我也尝试过 findBy 但随后它给出了错误说明 userType 没有这样的属性,它正在考虑 DeletedByUserType 两个不同的属性,这不应该是

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

java spring-boot hibernate jpa enums
1个回答
0
投票

也许可以在@Query 上尝试一下

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

而不是 o.PatentType = :PatentType

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

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