Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner join with select on HQL

I want to do something like that with HQL:

SELECT *
FROM tableA a
INNER JOIN (select fieldA, sum(fieldB) as sum from tableB) b
ON a.fieldA = b.fieldA and a.fieldC = b.sum;

But this gives an error:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: (...

There is any way to make this using HQL and Hibernate?

like image 671
Montolide Avatar asked Dec 20 '22 18:12

Montolide


1 Answers

try the native SQL solution approach:

need toimport this first:

import org.hibernate.SQLQuery;

then somewhere in your code:

SQLQuery query = session.createSQLQuery(
    "SELECT * FROM tableA a
    INNER JOIN 
    (SELECT fieldA, sum(fieldB) as sum from tableB) b
    ON a.fieldA = b.fieldA and a.fieldC = b.sum"
);

more on this link
and HERE ( Joins in Hibernate Query Language)

like image 135
John Woo Avatar answered Dec 24 '22 03:12

John Woo