我在下面的查询中有一个复杂的查询,但我需要投影这个DeploymentDto,我可以使用哪种方法进行投影?此查询位于扩展JpaRepository的接口类中。稍后在另一个类中,我需要在代码中使用结果列表。
基本上,我需要将我的原生查询映射到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]"发布于 2020-07-21 04:58:39
如果你的DTO是一个类,你需要使用@SqlResultSetMapping。不过,我还是推荐给make your DTO a Interface and use this as a projection。
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();
}https://stackoverflow.com/questions/63003105
复制相似问题