Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate - HQL joins on many clauses

I've been reading Hibernate documentation, but I haven't found anything that would explain how to do the following.

I have the following SQL code that I'm trying to convert to HQL:

SELECT {msg.*}, {cmd.*} 
FROM Schema.Messages AS msg 
  LEFT OUTER JOIN schema.send_commands AS cmd 
    ON cmd.message_key = msg.unique_key 
    AND ( lower(cmd.status) IN (lower('failed') ) ) 
WHERE msg.sequence_received < 10";

The mainissue I'm having is that I'm unable to have two clauses on a LEFT OUTER JOIN. HQL allows me to have ON cmd.message_key = msg.unique_key , but how do I add the AND clause 2?

like image 255
iliaden Avatar asked Jun 14 '11 14:06

iliaden


People also ask

Can we use join in HQL?

HQL Join : HQL supports inner join, left outer join, right outer join and full join. For example, select e.name, a.

Is subquery supported in HQL?

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. Note that subqueries can also utilize row value constructor syntax.

What is the difference between HQL and JPQL?

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.

How does Hibernate HQL work?

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.


1 Answers

You can add extra join conditions using with keyword, something like this (depends on your mapping):

SELECT m, c 
FROM Message m LEFT JOIN m.commands c WITH (lower(c.status) = 'failed')
WHERE m.sequenceReceived < 10

See also:

  • 16.3. Associations and joins
like image 195
axtavt Avatar answered Oct 22 '22 04:10

axtavt