【问题标题】:Mapping of Repository object with DTO Object存储库对象与 DTO 对象的映射
【发布时间】:2018-09-18 06:07:43
【问题描述】:

我有一个作用于 Table Ride 的 Repository 类。

@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;
}

上面的查询工作正常。 我在 Repository 类定义中使用了 Ride。 如果我在最后一行使用 Ride

List<Ride> findByMaxDuration();

效果很好。

但如果我在最后一行使用 TopDriverDTO,

 List<TopDriverDTO> findByMaxDuration();

然后它抛出 org.springframework.core.convert.ConverterNotFoundException。

我们不能映射这个吗?? 有什么帮助吗??

【问题讨论】:

  • 您可以使用ResultTransformer。你的场景很好地解释了here。你可以看看。
  • @ErfanAhmedEmon :它要求一些persistence.xml。我没有使用任何持久性 xml。所以,没有解决问题。

标签: mysql spring-boot spring-data-jpa


【解决方案1】:

如果我们检查 CrudRepository 的实现,它会扩展 Repository 接口

Spring Data 存储库抽象中的中心接口是 Repository(可能并不令人惊讶)。它需要域类来管理以及域类的 id 类型作为类型参数。

来自您的代码:

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

您指定此存储库的 域类RideId 类型Long

当您的查询返回结果时,您的存储库会发现结果的类型(TopDriverDTO)与定义的域类(Ride)不同,并将抛出上述错误。

【讨论】:

  • 我知道这一点。我已经在我的问题中解释了这一点。解决方案是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-22
  • 1970-01-01
  • 2019-09-19
  • 2023-03-15
相关资源
最近更新 更多