我面对这个错误
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);
型
预约类
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;
}
型
Stacktrace:
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]
型
2条答案
按热度按时间46qrfjad1#
一个快速的解决方案是,由于您无法直接返回Appointment基础实体,因为您正在请求似乎是未Map的值,因此可以使用构造函数查询让JPA返回POJO而不是托管实体:
字符串
然后,您需要Appointment类中的构造函数,它以查询中使用的相同顺序(和类型)接受所有这些值:
型
请记住,以这种方式获取的Appointment示例不是由JPA管理的,因此对它们的更改不会被跟踪和同步到数据库。您可能最好使用一个单独的视图对象,反映这个“appointment”数据实际上是appointment和AppointmentType数据,尽管我不确定为什么您不应该让Appointment拥有对Appointment Type的ManyToOne引用:
型
允许它与注解一起发送,因此类型名称,lengthInMinutes和任何其他属性总是可访问的-或者根据需要延迟获取。
xghobddn2#
我知道这已经很老了,但是.