[英]Spring Data dto projection method
I have a query in the below which is complicated but i need to project this DeploymentDto which method i can use for projection?我在下面有一个复杂的查询,但我需要将此 DeploymentD 投影到我可以使用哪种方法进行投影? This query is in interface class which extends JpaRepository.
此查询位于扩展 JpaRepository 的接口 class 中。 Later in another class i need to use resulting list in my code.
稍后在另一个 class 中,我需要在我的代码中使用结果列表。
Basically i need to map my native query into dto, so for query like mine how can i do it?基本上我需要将我的本机查询 map 转换为 dto,所以对于像我这样的查询我该怎么做? I did research but JPQL query for example uses simple native queries.
我做了研究,但 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();
I cannot use directly it like List findLatestDeployments();我不能像 List findLatestDeployments() 那样直接使用它; It gave an error like this:
它给出了这样的错误:
{"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]"
If your DTO is a class you need to use @SqlResultSetMapping
.如果您的 DTO 是 class 您需要使用
@SqlResultSetMapping
。 I would however recommend to make your DTO a Interface and use this as a projection .但是,我建议将您的 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.