hql converts to wrong sql: brackets trouble

Description

Original:
/* select
count
from
PrivateMessage model
where
(
(
model.senderDelete=:w3
and model.owner=:w4
)
or (
model.recipientDelete=:w6
and model.to=:w7
)
)
and (
lower(model.message) like :w9
or (
lower(model.owner.name) like :w11
or lower(model.to.name) like :w12
)
) */ select
count as col_0_0_
from
PRIVATE_MESSAGES privatemes0_
inner join
MESSAGES privatemes0_1_
on privatemes0_.id=privatemes0_1_.id cross
join
USERS user1_ cross
join
USERS user2_
where
privatemes0_1_.OWNER=user1_.id
and privatemes0_.TO_USER_ID=user2_.id
and (
privatemes0_.SENDER_DELETE=?
and privatemes0_1_.OWNER=?
or privatemes0_.RECIPIENT_DELETE=?
and privatemes0_.TO_USER_ID=?
)
and (
lower(privatemes0_1_.MSG) like ?
or lower(user1_.NAME) like ?
or lower(user2_.NAME) like ?
) limit ?

But SHOULD be:

select
count as col_0_0_
from
PRIVATE_MESSAGES privatemes0_
inner join
MESSAGES privatemes0_1_
on privatemes0_.id=privatemes0_1_.id cross
join
USERS user1_ cross
join
USERS user2_
where
privatemes0_1_.OWNER=user1_.id
and privatemes0_.TO_USER_ID=user2_.id
and (
(privatemes0_.SENDER_DELETE=?
and privatemes0_1_.OWNER=?)
(or privatemes0_.RECIPIENT_DELETE=?
and privatemes0_.TO_USER_ID=?)
)
and (
lower(privatemes0_1_.MSG) like ?
or lower(user1_.NAME) like ?
or lower(user2_.NAME) like ?
) limit ?

Environment

Mac 10.7.3
Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11D50b)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)

hibernate+jpa+spring

org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final
postgresql 8.4-701.jdbc4

Assignee

Unassigned

Reporter

AntonP

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure