QueryDSL Window functions

前端 未结 2 423
清歌不尽
清歌不尽 2021-01-11 23:15

How can I write a query using window functions and selecting all fields in QueryDSL? In the docs there is an example like this:

query.from(employee)
.list(SQ         


        
2条回答
  •  攒了一身酷
    2021-01-12 00:08

    JPAQuery supports only the expressivity of JPQL, so window functions are not supported, but paging should work using

    query.from(employee).orderBy(employee.id).limit(1)
    

    In case you need to use window functions and you need employee.name and employee.id out this should work

    NumberExpression rowNumber = SQLExpressions.rowNumber()
        .over()
        .partitionBy(employee.name)
        .orderBy(employee.id).as("rowNumber");
    
    query.select(employee.name, employee.id)
        .from(SQLExpressions.select(employee.name, employee.id, rowNumber)
                            .from(employee).as(employee))
        .where(Expressions.numberPath(Long.class, "rowNumber").eq(1L))
        .fetch();
    

提交回复
热议问题