找不到能够从类型 X 转换为类型 Y 的转换器

No converter found capable of converting from type X to type Y

我遇到了这个错误

 No converter found capable of converting from type [java.lang.Integer] to type [@org.springframework.data.jpa.repository.Query com.kevcode.saludxi.citasmcs.models.entity.Appointment] 

当我尝试在我的方法中使用 @Query 时。

方法:

@Query("SELECT a.id, a.appointmentDate, a.appointmentTypeId, at.name as appointmentTypeName, " +
            "a.feeValue, at.lengthInMinutes, a.medicId, a.patientId, a.symptomId" +
            " FROM Appointment a INNER JOIN AppointmentType at ON at.id = a.appointmentTypeId" +
            " WHERE a.appointmentDate >= ?1 AND a.appointmentDate <=  ?2 AND a.medicId = ?3")
    List<Appointment> findAllBetweenDateAndMedicId(Date maxDate, Date minDate, int medicId);

约会Class

public class Appointment extends EntityBase {
    //private int id **extended from EntityBase**
    private int appointmentTypeId;
    private int symptomId;
    private int medicId;
    private int patientId;
    @Column(name="appointmet_date")
    private LocalDateTime appointmentDate;
    private float feeValue;
    @Transient
    private int lengthInMinutes;
    @Transient
    private String appointmentTypeName;
}

堆栈跟踪:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [@org.springframework.data.jpa.repository.Query com.kevcode.saludxi.citasmcs.models.entity.Appointment] for value '{78532, 2022-05-24 00:46:14.0, 2, Examen, 3000.0, 30, 61, 21, 41}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [@org.springframework.data.jpa.repository.Query com.kevcode.saludxi.citasmcs.models.entity.Appointment]

一个快速的解决方案是使用构造函数查询来代替 JPA return POJO,因为您不能在请求看似未映射的值时直接 return 指定基础实体托管实体:

@Query("SELECT new package.Appointment(a.id, a.appointmentDate, a.appointmentTypeId, at.name, " +
            "a.feeValue, at.lengthInMinutes, a.medicId, a.patientId, a.symptomId)" +
            " FROM Appointment a INNER JOIN AppointmentType at ON at.id = a.appointmentTypeId" +
            " WHERE a.appointmentDate >= ?1 AND a.appointmentDate <=  ?2 AND a.medicId = ?3")

然后您需要约会 class 中的构造函数,它以查询中使用的相同顺序(和类型)接收所有这些值:

public class Appointment extends EntityBase {
    //private int id **extended from EntityBase**
    private int appointmentTypeId;
    private int symptomId;
    private int medicId;
    private int patientId;
    @Column(name="appointmet_date")
    private LocalDateTime appointmentDate;
    private float feeValue;
    @Transient
    private int lengthInMinutes;
    @Transient
    private String appointmentTypeName;
    public Appointment(int id, LocalDateTime appointmentDate, int appointmentTypeId, String appointmentTypeName, float feeValue, int lengthInMinutes, int medicId, int patientId, int symptomId){
      this.setId(id);
      this.appointmentDate = appointmentDate;
      ..
    }
}

请记住,以这种方式获取的 Appointment 实例不受 JPA 管理,因此不会跟踪对它们的更改并将其同步到数据库。您最好使用一个单独的视图对象来反映此 'appointment' 数据实际上是约会和 AppointmentType 数据,但我不确定为什么您不应该只让 Appointment 具有对 AppointmentType 的 ManyToOne 引用:

public class Appointment extends EntityBase {
  ..
  @ManyToOne
  @JoinColumn(name="appointmentTypeId")
  private AppointmentType appointmentType;
}

允许它与约会一起发送,因此类型名称、lengthInMinutes 和任何其他属性始终可访问 - 或者按需延迟获取。