Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brackets in HQL not ntranslated to SQL

Tags:

java

hibernate

Here is my HQL:

Query query = createQueryOnCurrentSession("DELETE Email e " +
                "where " +
                "(status = :sent and creationTime <= :creation)" +
                "or " +
                "(status = :error and maxEttempts >= :maxEttempts)");

Here is the generated SQL:

delete from `email` where `status`=? and `creation_time`<=? or `status`=? and `attempts`>=?

Question: why are the brackets not in the SQL? I would expect it to be:

delete from `email` where (`status`=? and `creation_time`<=?) or (`status`=? and `attempts`>=?)

may be as alternative I will delete in 2 requests?

delete from `email` where `status`=? and `creation_time`<=?
delete from `email` where `status`=? and `attempts`>=?
like image 211
urir Avatar asked Dec 17 '12 07:12

urir


People also ask

What is HQL write difference between HQL and SQL?

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.

How HQL is different from SQL?

Unlike SQL, HQL uses classes and properties in lieu of tables and columns. HQL supports polymorphism as well as associations, which in turn allows developers to write queries using less code as compared to SQL.

Is HQL like SQL?

HQL or Hibernate Query Language is the object-oriented query language of Hibernate Framework. HQL is very similar to SQL except that we use Objects instead of table names, that makes it more close to object oriented programming.


1 Answers

It's actually a feature.

Since and has precedence over or, hibernate knows it, and removes brackets.

You don't need those brackets there.

like image 107
Jaanus Avatar answered Sep 20 '22 15:09

Jaanus