简体   繁体   English

如何从 Spring Data JPA GROUP BY 查询中实现 ORDER BY

[英]How to implement ORDER BY from a Spring Data JPA GROUP BY query

Based on question How to return a custom object from a Spring Data JPA GROUP BY query , my custom query is working, as follows:基于问题How to return a custom object from a Spring Data JPA GROUP BY query ,我的自定义查询正在运行,如下所示:

@Query("select new br.com.cwidevs.dto.Goleador(j.id, sum(pj.gols)) from PartidaJogador pj join pj.id.jogador j group by j.id")
public List<Goleador> getGoleadores();

Here is the simple bean class:这是简单的 bean 类:

public class Goleador {

    private Long jogador;

    private Long totalGols;    
}

At this point, how to implement ORDER BY ?此时,如何实现ORDER BY Can I achieve thi with Sort ?我可以用Sort实现吗?

Try this:试试这个:

@Entity
public class Goleador {
    //...
}

@Entity
public class PartidaJogador {
    //...

    @ManyToOne
    private Goleador jogador;

    private Long gols;

    //...
}

// DTO as Projection
public interface GoleadorWithGols {
    Goleador getGoleador();
    Long getTotalGols()
}

public interface GoleadorRepo extends JpaRepository<Goleador, Long> {
    @Query("select j as goleador, sum(pj.gols) as totalGols from PartidaJogador pj join pj.jogador j group by j order by totalGols")
    List<GoleadorWithGols> getGoleadores();
}

Here we use a projection GoleadorWithGols as DTO to get necessary data.这里我们使用投影GoleadorWithGols作为 DTO 来获取必要的数据。

More info is here .更多信息在这里

JpaRepository extends PagingAndSortingRepository so you can definetly use Sort JpaRepository扩展了 PagingAndSortingRepository所以你可以明确地使用Sort

Add parameter to your method将参数添加到您的方法
public List<Goleador> getGoleadores(Sort sort);

when you call it just specify by which column you want to sort your query and you should be good.当你调用它时,只需指定你想按哪一列对查询进行排序,你就可以了。

repoGoleador.getGoleadores(new Sort("jogador"));  

I just solved this problem:我刚刚解决了这个问题:

Class-based Projections doesn't work with query基于类的Projections不适用于查询

native(@Query(value = "SELECT ...", nativeQuery = true))

So I recommend to define custom DTO using interface.所以我建议使用接口定义自定义DTO Before using DTO should verify the query syntactically correct or not.在使用DTO之前应该验证查询在语法上是否正确。

暂无
暂无

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

相关问题 如何从 Spring Data JPA GROUP BY 查询返回自定义对象 - How to return a custom object from a Spring Data JPA GROUP BY query 如何按Spring数据JPA查询中的多列排序并按顺序进行计算? - How to order by multiple columns in Spring data JPA query with calculation in order by? 如何使用 Spring 数据 Jpa 或 Hibernate 实现 ALTER TABLE 查询 - How to implement ALTER TABLE Query using Spring Data Jpa or Hibernate 如何使用 spring-data-jpa 2.1 实现长/复杂查询 - How to implement long/complex query with spring-data-jpa 2.1 Spring Data JPA-从组查询返回对象 - Spring data JPA - return an Object from a Group Query 如何使用带有“从方法名创建查询”策略的Spring数据JPA来实现这两个简单查询? - How can I implement these 2 simple queries using Spring data JPA with the “query creation from method names” strategy? Spring JPA + Mysql,如何实现 Z63225F19FCCB18E7C709F1FAHEREZ FHEREZ 查询73? - Spring JPA + Mysql, how to implement a SELECT, FROM, WHERE query? Spring 数据 JPA Java - 如何从查询中获取最后 10 条记录并按列排序 - Spring Data JPA Java - how to get the last 10 records from query and order by column 如何从具有多个计数和 Group by 查询的 Spring Data JPA 返回可分页的自定义对象? - How to return a pageable custom object from a Spring Data JPA with multiple counts and Group by query? 如何从 Spring 数据 JPA GROUP BY 查询返回对象列表而不是对象数组 - How to return a List of Objects instead of Array of Objects from a Spring Data JPA GROUP BY query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM