У меня есть класс репозитория, который действует на Table Ride.
@RestResource(exported = false)
public interface RideRepository extends CrudRepository<Ride, Long> {
@Query(value = "select p.*, top5.duration_seconds / 1 as totalRideDurationInSeconds, top5.max_seconds / 1 as maxRideDurationInSeconds, " +
"top5.avg_distance as averageDistance , top5.driver_id as driver_id,top5.rider_id as rider_id, top5.start_time as start_time, top5.end_time as end_time, top5.distance as distance " +
"from (select r.driver_id as driver_id, r.start_time as start_time, r.end_time as end_time, r.distance as distance, r.rider_id as rider_id, "+
"avg(r.distance) as avg_distance, " +
"sum(to_seconds(r.end_time) - to_seconds(r.start_time)) as duration_seconds, "+
"max(to_seconds(r.end_time) - to_seconds(r.start_time)) as max_seconds "+
"from ride r "+
"where r.start_time >= '2018-08-08T12:12:12' and "+
"r.end_time <= '2018-08-08T18:12:12' "+
"group by r.driver_id "+
"order by duration_seconds desc "+
"limit 5 "+
") top5 join "+
"person p "+
"on top5.driver_id = p.id" , nativeQuery = true)
List<TopDriverDTO> findByMaxDuration();
TopDriverDTO - это объект модели:
public class TopDriverDTO {
public TopDriverDTO(String name,
String email,
Long totalRideDurationInSeconds,
Long maxRideDurationInSeconds,
Double averageDistance) {
this.setName(name);
this.setEmail(email);
this.setAverageDistance(averageDistance);
this.setMaxRideDurationInSecods(maxRideDurationInSeconds);
this.setTotalRideDurationInSeconds(totalRideDurationInSeconds);
}
public TopDriverDTO() {
}
private String name;
private String email;
private Long totalRideDurationInSeconds;
private Long maxRideDurationInSeconds;
private Double averageDistance;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getTotalRideDurationInSeconds() {
return totalRideDurationInSeconds;
}
public void setTotalRideDurationInSeconds(Long totalRideDurationInSeconds) {
this.totalRideDurationInSeconds = totalRideDurationInSeconds;
}
public Long getMaxRideDurationInSecods() {
return maxRideDurationInSeconds;
}
public void setMaxRideDurationInSecods(Long maxRideDurationInSeconds) {
this.maxRideDurationInSeconds = maxRideDurationInSeconds;
}
public Double getAverageDistance() {
return averageDistance;
}
public void setAverageDistance(Double averageDistance) {
this.averageDistance = averageDistance;
}
Ride.java:
@Entity
@Table(name = "ride")
public class Ride implements Serializable{
private static final long serialVersionUID = 9097639215351514001L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@NotNull
@Column(name = "start_time")
String startTime;
@NotNull
@Column(name = "end_time")
String endTime;
@Column(name = "distance")
Long distance;
@ManyToOne
@JoinColumn(name = "driver_id", referencedColumnName = "id")
Person driver;
@ManyToOne
@JoinColumn(name = "rider_id", referencedColumnName = "id")
Person rider;
Он также имеет все методы getter, setter.
p - модель Person.
@Entity
@Table(name = "person")
public class Person implements Serializable{
private static final long serialVersionUID = 7401548380514451401L;
public Person() {}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@Column(name = "name")
String name;
@NotNull
@Email
@Column(name = "email")
String email;
@Column(name = "registration_number")
String registrationNumber;
}
Запрос выше работает нормально.Я использовал Ride в определении класса Repository.Если я использую Ride в последней строке
List<Ride> findByMaxDuration();
Это работает прекрасно.
Но если я использую TopDriverDTO в последней строке,
List<TopDriverDTO> findByMaxDuration();
, тогда он генерирует org.springframework.core.convert.ConverterNotFoundException.
Разве мы не можем отобразить это ??Любая помощь ??