Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join without association in HQL

Lets say I have two tables(A, B) like:

A {id, a, c} B {id, b, c} 

I have also their entities. I want to write an HQL so that the result set will be like (where A.c = B.c):

(a1, b1, c1) (a2, b2, c2) (a3, b3, c3) ... 

Since on clauses are not supported by hibernate I am stuck and I don't know how to write the query.

like image 728
nimcap Avatar asked Jun 10 '09 07:06

nimcap


People also ask

Can we use join in HQL query?

Some of the commonly supported clauses in HQL are: HQL From: HQL From is same as select clause in SQL, from Employee is same as select * from Employee . We can also create alias such as from Employee emp or from Employee as emp . HQL Join : HQL supports inner join, left outer join, right outer join and full join.

How do I join two tables in HQL?

We can apply the Joins in Hibernate by using the HQL query or native SQL query. To make a join between the two tables, the two tables must be in a logical relationship. We can achieve the relationship between two tables by applying the parent table's primary key as a child table's foreign key.

Why does hibernate use cross join?

Hibernate transforms this into a cross join which creates the same result as an inner join when you add the join condition to the WHERE statement. This approach has two disadvantages: It is not as easy to read as an inner join especially if the WHERE statement gets complex and is often the cause of additional bugs.

What is the difference between JPQL and HQL?

The Hibernate Query Language (HQL) and Java Persistence Query Language (JPQL) are both object model focused query languages similar in nature to SQL. JPQL is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL query, the reverse is not true however.


1 Answers

You have to use the cross join notation:

from A as table_a , B as table_b where table_a.c = table_b.c 

Of course there is no way to implement outer joins in this manner, so you might have some trouble if that's your case.

For the analogous case with criteria refer to: hibernate-criteria-joining-table-without-a-mapped-association

like image 137
Il-Bhima Avatar answered Sep 27 '22 22:09

Il-Bhima