Mysql 存储库对象与DTO对象的映射
我有一个作用于Table Ride的Repository类Mysql 存储库对象与DTO对象的映射,mysql,spring-boot,spring-data-jpa,Mysql,Spring Boot,Spring Data Jpa,我有一个作用于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 /
@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();
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-in存储库类定义。
如果我在最后一行使用骑乘
List<Ride> findByMaxDuration();
它很好用
但如果我在最后一行用TopDriverDTO
List<TopDriverDTO> findByMaxDuration();
然后抛出org.springframework.core.convert.ConverterNotFoundException
我们不能映射这个吗??
有什么帮助吗???如果我们检查crudepository的实现,它会扩展存储库接口 Spring数据存储库抽象的中心接口是存储库,这可能并不令人惊讶。它接受要管理的域类以及域类的id类型作为类型参数 从您的代码:
public interface RideRepository extends CrudRepository<Ride, Long>...
您正在指定此存储库的域类为Ride,Id类型为Long
当查询返回结果时,存储库将发现结果的类型与定义的域类不同,并将抛出所述错误。您可以使用ResultTransformer。你的情况解释得很好。您可以看一看。@ErfanAhmedEmon:它在询问persistence.xml。我没有使用任何持久性xml。所以,没有解决问题。我知道这一点。我已经在我的问题中解释了这一点。解决这个问题的办法是什么?
public interface RideRepository extends CrudRepository<Ride, Long>...