我有一个Repository类,它作用于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是人员模型。
@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。 如果我在最后一行中使用Ride
List<Ride> findByMaxDuration();
效果很好。
但是如果我在最后一行中使用TopDriverDTO,
List<TopDriverDTO> findByMaxDuration();
然后它会抛出org.springframework.core.convert.ConverterNotFoundException。
我们不能映射这个吗? 有帮助吗?
答案 0 :(得分:0)
如果我们检查 CrudRepository 的实现,它将扩展 Repository 接口
Spring数据存储库抽象中的中央接口是Repository(可能并不令人惊讶)。它需要域类以及域类的id类型作为类型参数来进行管理。
根据您的代码:
public interface RideRepository extends CrudRepository<Ride, Long>...
您指定此存储库的 Domain类为乘车,且 Id类型为 Long 。< / p>
当查询返回结果时,您的存储库会发现结果与定义的域类( Ride )具有不同的类型( TopDriverDTO ),并且会抛出所述错误。