repository对象与dto对象的Map

kiayqfof  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(336)

我有一个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;
  }

骑乘.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-in存储库类定义。如果我用最后一行

List<Ride> findByMaxDuration();

很好用。
但如果我在最后一行用topdriverdto,

List<TopDriverDTO> findByMaxDuration();

然后抛出org.springframework.core.convert.converternotfoundexception。
我们不能绘制这个Map吗??有什么帮助吗??

jogvjijk

jogvjijk1#

如果我们检查crudrepository的实现,它将扩展存储库接口
spring数据存储库抽象的中心接口是repository(可能不太奇怪)。它接受要管理的域类以及域类的id类型作为类型参数。
从您的代码:

public interface RideRepository extends CrudRepository<Ride, Long>...

您正在指定此存储库的域类的id类型为long。
当查询返回结果时,存储库将发现结果的类型(topdriverdto)与定义的域类(ride)不同,并将抛出上述错误。

相关问题