Search code examples
javaspring-boothibernatejpaenums

Enum value giving jdbcMapping error while using query in springboot


My requirement is to find entry by patientId, patientType and DeletedAt, DeletedBy, DeletedByUserType these three should be null

I used this query but it is not able to map patientType which is a ENUM

Error

"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"

Query

 @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 Class

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

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

Entity Class

private class InsuranceEntity{
   @Enumerated(EnumType.STRING)
    @Column(name = "patientType", length = 3, nullable = false)
    private PatientTypeEnum patientType;
}

Solution

  • Maybe try this on the @Query

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

    instead of o.patientType = :patientType

    Reference: Can I use enum parameter into JpaRepository nativeQuery?