Mistre83
Mistre83

Reputation: 2827

Java Hibernate - Wrong parenthesis in query

In my function I created a condition like this:

and (o.status != :shipped or (o.status = :cancelled and sp.cancelledQt != sp.requestedQt))

If I enable query logging this condition becomes:

and (shipmentor0_.status<>? or shipmentor0_.status=? and positions1_.cancelledQt<>positions1_.requestedQt)

As you can see the parenthesis are in a different order than I wrote.

What am I doing wrong?

Upvotes: 0

Views: 513

Answers (2)

Sterconium
Sterconium

Reputation: 579

Nothing.

Hibernate it's just taking advantage of the distributive property of the logical disjunction (fancy name for "OR") to redact your query in a more efficient way.

It is one of the pros of using an ORM instead of plain JDBC.

Upvotes: 0

Victor Soares
Victor Soares

Reputation: 880

This is because the internal parenthesis is not necessary. The "And" operator precedence is higher then the "Or" operator. But you can keep it to make it more readable.

Upvotes: 1

Related Questions