繁体   English   中英

Spring 数据dto投影法

[英]Spring Data dto projection method

我在下面有一个复杂的查询,但我需要将此 DeploymentD 投影到我可以使用哪种方法进行投影? 此查询位于扩展 JpaRepository 的接口 class 中。 稍后在另一个 class 中,我需要在我的代码中使用结果列表。

基本上我需要将我的本机查询 map 转换为 dto,所以对于像我这样的查询我该怎么做? 我做了研究,但 JPQL 查询例如使用简单的本机查询。

@Query(value = "SELECT a.name, a.created_at, a.updated_at, d.version, a.image, d.id, d.app_id\n" +
            "FROM applications a\n" +
            "LEFT JOIN deployments d ON d.app_id = a.id\n" +
            "WHERE d.app_id IS NULL\n" +
            "union\n" +
            "select a.name, d.created_at, d.updated_at, d.version, a.image, d.id, d.app_id from applications a\n" +
            "inner join deployments d on d.app_id = a.id\n" +
            "where d.updated_at = (\n" +
            "select max(d1.updated_at) from deployments d1 where d1.app_id = a.id)\n" +
            "ORDER BY name",  nativeQuery = true)
    List<Deployment> findLatestDeployments();

我不能像 List findLatestDeployments() 那样直接使用它; 它给出了这样的错误:

{"timestamp":1595277133888,"message":"No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.gg.applications.model.DeploymentDto]"

如果您的 DTO 是 class 您需要使用@SqlResultSetMapping 但是,我建议将您的 DTO 设为 Interface 并将其用作投影

interface Deployment{
  public String name();
  public LocalDateTime created_at();
  public LocalDateTime updated_at();
  public long version();
  public byte[] image();
  public Long id();
  public Long app_id();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM