Union in JPA query - from the same table

前端 未结 2 1510
渐次进展
渐次进展 2021-01-19 17:51

I have a requirement where I need to restrict number of records returned from a table for a particular flag and all records for the other flag value.

For example:

2条回答
  •  轮回少年
    2021-01-19 18:08

    No, this is not possible with JPQL, because it does not have UNION (ALL). Additionally there is no way to limit amount of rows returned in query string itself with rownum, but that is done via setMaxResults.

    In many situations

    • executing two queries,
    • limiting number of results in first one with setMaxResults, and
    • discarding duplicates and combining results of both queries in Java

    is viable solution.

提交回复
热议问题