Spring Data数据到投影方法



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

基本上,我需要将我的原生查询映射到dto中,所以对于像我这样的查询,我该怎么做呢?我做了一些研究,但JPQL查询使用了简单的本地查询。

@Query(value = "SELECT a.name, a.created_at, a.updated_at, d.version, a.image, d.id, d.app_idn" +
"FROM applications an" +
"LEFT JOIN deployments d ON d.app_id = a.idn" +
"WHERE d.app_id IS NULLn" +
"unionn" +
"select a.name, d.created_at, d.updated_at, d.version, a.image, d.id, d.app_id from applications an" +
"inner join deployments d on d.app_id = a.idn" +
"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是一个类,则需要使用@SqlResultSetMapping。然而,我建议您将DTO作为一个接口,并将其用作投影。

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();
}

相关内容

最新更新