AND / OR & parentheses

3,413 views
Skip to first unread message

Frans

unread,
Aug 27, 2013, 11:14:20 AM8/27/13
to quer...@googlegroups.com
Hi,

I'm using Querydsl 3.2.2. with Hibernate 4.2.4 and MySQL and playing with several logical expressions combined with AND / OR operators.

I'm wondering why BooleanExpression.orAllOf() outputs parentheses and BooleanExpression.andAnyOf() does not.

For example:

    JPAQuery = ...
    QAccountEntity account = ...
    query.where(account.name.isNotNull().andAnyOf(account.name.eq("alpha"), account.name.eq("beta")));

Generates the following JPQL snippet (as expected):

    where a.name is not null and (a.name = ?1 or a.name = ?2)

But if I write

    JPAQuery = ...
    QAccountEntity account = ...
    query.where(account.name.isNotNull().orAllOf(account.name.eq("alpha"), account.name.eq("beta")));

I have:

    where a.name is not null or a.name = ?1 and a.name = ?2

I guess the answer is operator precedence in JPQL (OR has lower precedence than AND), but IMHO I find it error prone: 1) when reading it (debugging purposes) and 2) hibernate does not seem to respect the operator precedence.
Is there a way to force use of parentheses or is it something you might consider adding?

Thanks a lot.

Frans


Timo Westkämper

unread,
Aug 27, 2013, 11:54:29 AM8/27/13
to Querydsl on behalf of Frans
Hi.

On Tue, Aug 27, 2013 at 6:14 PM, Frans via Querydsl <querydsl+noreply-APn2wQe26Ka2Q9t...@googlegroups.com> wrote:
Hi,

I'm using Querydsl 3.2.2. with Hibernate 4.2.4 and MySQL and playing with several logical expressions combined with AND / OR operators.

I'm wondering why BooleanExpression.orAllOf() outputs parentheses and BooleanExpression.andAnyOf() does not.

For example:

    JPAQuery = ...
    QAccountEntity account = ...
    query.where(account.name.isNotNull().andAnyOf(account.name.eq("alpha"), account.name.eq("beta")));

Generates the following JPQL snippet (as expected):

    where a.name is not null and (a.name = ?1 or a.name = ?2)

But if I write

    JPAQuery = ...
    QAccountEntity account = ...
    query.where(account.name.isNotNull().orAllOf(account.name.eq("alpha"), account.name.eq("beta")));

I have:

    where a.name is not null or a.name = ?1 and a.name = ?2

I guess the answer is operator precedence in JPQL (OR has lower precedence than AND), but IMHO I find it error prone: 1) when reading it (debugging purposes) and 2) hibernate does not seem to respect the operator precedence.

I find it more readable with minimal unnecessary parentheses.

How does Hibernate not respect operator precedence? As far as I know and/or precedence is the same for JPQL and many SQL dialects.
 
Is there a way to force use of parentheses or is it something you might consider adding?

It is possible, but not really recommended, by subclassing JPQLTempaltes and customizing the templates for and and or operations.

Br,
Timo
 

Thanks a lot.

Frans


--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com


Frans

unread,
Aug 27, 2013, 12:18:31 PM8/27/13
to quer...@googlegroups.com
Hi,


On Tuesday, August 27, 2013 5:54:29 PM UTC+2, Timo Westkämper wrote:
Hi.


On Tue, Aug 27, 2013 at 6:14 PM, Frans via Querydsl <querydsl+noreply-APn2wQe26Ka2Q9tImJZ8NkTb1mX8KUHVTNI0z2rlQDYm1dH@googlegroups.com> wrote:
Hi,

I'm using Querydsl 3.2.2. with Hibernate 4.2.4 and MySQL and playing with several logical expressions combined with AND / OR operators.

I'm wondering why BooleanExpression.orAllOf() outputs parentheses and BooleanExpression.andAnyOf() does not.

For example:

    JPAQuery = ...
    QAccountEntity account = ...
    query.where(account.name.isNotNull().andAnyOf(account.name.eq("alpha"), account.name.eq("beta")));

Generates the following JPQL snippet (as expected):

    where a.name is not null and (a.name = ?1 or a.name = ?2)

But if I write

    JPAQuery = ...
    QAccountEntity account = ...
    query.where(account.name.isNotNull().orAllOf(account.name.eq("alpha"), account.name.eq("beta")));

I have:

    where a.name is not null or a.name = ?1 and a.name = ?2

I guess the answer is operator precedence in JPQL (OR has lower precedence than AND), but IMHO I find it error prone: 1) when reading it (debugging purposes) and 2) hibernate does not seem to respect the operator precedence.

I find it more readable with minimal unnecessary parentheses.

How does Hibernate not respect operator precedence? As far as I know and/or precedence is the same for JPQL and many SQL dialects.

I made more tests and you are right, Hibernate does respect it. So I won't change templates just for debugging purposes.
Thanks.
 
Reply all
Reply to author
Forward
0 new messages