HQL Unions

Description

Add the ability to define unions in HQL. Support will be initially limited to only:
1) scalar queries : select id from Animal union select id from Car
2) the same entity : from Animal where ... union from Animal where ...

Support both UNION and UNION ALL

Activity

Show:

Christian Beikov March 15, 2021 at 9:45 AM

Support for set operations (UNION, INTERSECT, EXCEPT) was added in Hibernate 6.0.

Steve Ebersole November 23, 2019 at 2:40 PM

The #unionQuery is interesting idea.

I do think it will be possible to implement UNION as a HQL language feature though given the design of SQM and the SQL AST (internal “query syntax trees”). Possibly even using the support for CTE statements - see org.hibernate.query.sqm.tree.cte.SqmCteStatementand org.hibernate.sql.ast.tree.cte.CteStatement

Luis Carlos Limas Alvarez October 18, 2016 at 7:04 PM
Edited

As a suggestion... you can skip HQL validation by adding an intermediate function called "unionQuery", the suggested sintax would be like this:

That way you can reuse the same HQL validation and the only left thing to do is to validate each query returned values/types;

I fixed this for my requirement by now doing the following (i'm simplifying a lot this example, is only to give you the idea):

Christian Beikov October 11, 2016 at 10:38 AM

In the meantime you could also try out Blaze-Persistence if you really need support for SET functions but don't want to leave your domain model thinking. It works on top of JPA but the integration is currently for Hibernate only.

Steve Ebersole November 4, 2015 at 11:34 PM

If UNION support makes it into HQL grammars, it will happen as part of the work being done under https://github.com/hibernate/hibernate-semantic-query which has been discussed alot on the hibernate-dev mailing list. If anyone is interested in helping with that effort it will help get things like this implemented sooner. If not, it gets done as I have time between fixing bugs and other work.

In the meantime, per the incomplete support for UNION in the current grammars will be removed in 5.0.4. Its incomplete and causes problems in recognizing certain valid queries.

Fixed

Details

Assignee

Reporter

Components

Priority

Created June 30, 2005 at 7:49 PM
Updated March 18, 2021 at 7:49 PM
Resolved March 15, 2021 at 9:45 AM