Does anybody have any idea about unions in hibernate?
I got this question in a interview. My guess is yes, but I'm not sure where and when.
I know hibernate does not support union queries at the moment, right now the only way I see to make a union is to use a view table.
The following are some of the reasons why HQL is preferred over SQL: Provides full support for relational operations. It is possible to represent SQL Queries in the form of objects in HQL which uses classes and properties instead of tables and columns. Return results as objects.
You can use native SQL to express database queries if you want to utilize database-specific features such as query hints or the CONNECT keyword in Oracle. Hibernate 3. x allows you to specify handwritten SQL, including stored procedures, for all create, update, delete, and load operations.
A subquery must be surrounded by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed. Note that HQL subqueries can occur only in the select or where clauses.
The answer is no... see here https://www.hibernate.org/117.html#A21
Even though you can't do a true union, you can simulate one if the union is for two different queries for the same entity. In that case, you can use a Disjunction
to OR two or more different DetachedCriteria
queries, using the output of those queries as inputs to a Restrictions.in
against some column in the main entity. This method gets you the equivalent of a union.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With