Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform left join in Hibernate Query Language?

This is my HQL query, but it isn't working and is throwing an error.

Hql query:

SELECT 
    *
FROM 
    TABLEA A 
LEFT JOIN 
    A.TABLEB B 
WHERE 
    A.COLUMNNAME = B.COLUMNAME

and it causes this error:

org.hibernate.QueryException:
This Query caught Exception. could not resolve property: of TABLEB:TABLEA.

How can I solve this problem? Actually I retrieved a value from more than one table. This query doesn't work with CreateQuery(strQuery).

like image 605
karthik.A.M Avatar asked Aug 31 '13 06:08

karthik.A.M


People also ask

How do I use left join in HQL?

select * from A as a left join B as b on a.id = b.id left join C as c on b. type=c. type; Need help in writing equivalent HQL.

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.

What is left join fetch in Hibernate?

JOIN FETCH (or LEFT JOIN FETCH ) will collect all the associations along with their owner object. Meaning that the collection will be retrieved in the same select. This can be shown by enabling Hibernate's statistics. A (left/outer) join fetch is great for *ToOne (many-to-one or one-to-one) associations.


1 Answers

In HQL you can use LEFT JOIN only with linked property in main entity:

Sample

EntityA has an object entityB of type EntityB so you can

SELECT A FROM EntityA A LEFT JOIN A.entityB B WHERE ...

IF EntityA haven't entityB property but is EntityB have a property entityA, you can't write this:

SELECT A FROM EntityA LEFT JOIN EntityB B WHERE B.entityA = A 

because you have an error. This is an Hibernate issue not resolved yet.

like image 64
Joe Taras Avatar answered Oct 10 '22 19:10

Joe Taras