Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use union in hibernate?

Tags:

hibernate

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.

like image 917
gkpstar Avatar asked Jan 10 '10 02:01

gkpstar


People also ask

Does hibernate support union HQL query?

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.

Why we use HQL instead of SQL?

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.

What is native query in hibernate?

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.

How do you write a subquery in HQL?

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.


2 Answers

The answer is no... see here https://www.hibernate.org/117.html#A21

like image 87
Chris Conway Avatar answered Oct 23 '22 04:10

Chris Conway


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.

like image 40
atrain Avatar answered Oct 23 '22 04:10

atrain