-
Hi, I'm trying to use union but I'm having some problems, maybe a bug or there is something I'm doing wrong.
This is the query printed with getQueryString(): SELECT root, (SELECT COUNT(*) FROM ApplicationServer applicationServer) AS total, 0L AS hasPrevious, 0L AS hasNext FROM ApplicationServer root WHERE root.applicationId = :param_0
UNION ALL
SELECT root, (SELECT COUNT(*) FROM ApplicationServer applicationServer) AS total, 0L AS hasPrevious, 0L AS hasNext FROM ApplicationServer root WHERE root.applicationId = :param_1 I would like to build something like this (with order by and limit): SELECT root, (SELECT COUNT(*) FROM ApplicationServer applicationServer) AS total, 0L AS hasPrevious, 0L AS hasNext FROM ApplicationServer root WHERE root.applicationId = :param_0 ORDER BY root.createdAt ASC LIMIT 10
UNION ALL
SELECT root, (SELECT COUNT(*) FROM ApplicationServer applicationServer) AS total, 0L AS hasPrevious, 0L AS hasNext FROM ApplicationServer root WHERE root.applicationId = :param_1 ORDER BY root.createdAt ASC LIMIT 10 I'm using H2 and Oracle. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
Well, I found an alternative using unions in the where clause and some tricks because build dynamic queries is hard, I show an example: CriteriaBuilder<Tuple> cb = cbf.create(em, Tuple.class)
.from(ApplicationServer.class, "rootFrom")
.select("rootFrom")
.selectSubquery("total")
.from(ApplicationServer.class)
.select("COUNT(*)")
.where("applicationId").eqExpression("rootFrom.applicationId")
.end()
.orderByDesc("id")
.where("id").in()
.from(ApplicationServer.class)
.where("applicationId").eq(1L)
.select("id")
.orderByDesc("id")
.setMaxResults(2)
.union()
.from(ApplicationServer.class)
.where("applicationId").eq(2L)
.select("id")
.orderByDesc("id")
.setMaxResults(2)
.union()
.from(ApplicationServer.class)
.where("applicationId").eq(3L)
.select("id")
.orderByDesc("id")
.setMaxResults(2)
.endSet()
.end()
; Now I dont need to use |
Beta Was this translation helpful? Give feedback.
-
This was a bug and was fixed as part of #1274 |
Beta Was this translation helpful? Give feedback.
-
Hi @beikov I want to make a query like that but dinamically but i'm not having success. I keep getting the following error all the time: Modifications to a query after connecting with a set operation is not allowed! Is possible to make this query dinamically (subqueries depending on a list)? Thanks in advance |
Beta Was this translation helpful? Give feedback.
This was a bug and was fixed as part of #1274