Hello everyone.

I'm a developer in a project where we are using OpenJPA and I've run into 
a problem where OpenJPA seems to remove parenthesis from the query.

The original query that we create looks like this:
Select Distinct a From HtAnsokanEO a JOIN a.arenden b JOIN b.handlaggare c 
Where a.ansokanNr = '123456' And ((b.status = ?1 And c.opid = 'ugwo') Or 
b.status <> ?2)

The where clause is supposed to select all rows with the status "sparat" 
that belong to me or the rows where status is something other than 
"sparat".

What JPA in turn executes against the Oracle server, according to the 
trace, is the following:
(part of the trace is omitted for brevity)
     FROM T_HT_ANSOKAN t0  ,
    T_HT_ARENDE t1         ,
    T_HT_ANVANDARE t2      ,
    T_HT_UTFORARE t3       ,
    T_HT_UTFORARE_SVENSK t4,
    T_HT_UTFORARE_UTLANDSK t5
    WHERE (t0.ANSOKAN_NR = ?
  AND (t1.status         = ?
  AND t1.HANDLAGGARE_ID  = ?
  OR t1.status          <> ?)
  AND 1                  = 1)
  AND t0.id              = t1.ANSOKAN_ID
  AND t0.UTFORARE_ID     = t3.id(+)
  AND t1.HANDLAGGARE_ID  = t2.opid
  AND t3.id              = t4.id(+)
  AND t3.id              = t5.id(+)
  )
  WHERE ROWNUM <= ? [params=(String) 123456                            ,
  (String) SPARAT                                                      ,
  (String) ugwo                                                        ,
  (String) SPARAT                                                      ,
  (LONG) 51] 641289 ht_junit TRACE [main] openjpa.jdbc.SQL - <t 6889270,
  conn 12833564 

The interesting part is the WHERE-part:
WHERE (t0.ANSOKAN_NR = ?
  AND (t1.status         = ?
  AND t1.HANDLAGGARE_ID  = ?
  OR t1.status          <> ?)
  AND 1                  = 1)

I would have expected something like this (with two extra paranthesis):
WHERE (t0.ANSOKAN_NR = ?
  AND ((t1.status         = ?
  AND t1.HANDLAGGARE_ID  = ? )
  OR t1.status          <> ?)
  AND 1                  = 1)

Do you have any clue as to how we could regain these lost parenthesis? Our 
problem is that this actually seems to change the behavior of the Query 
and cause unexpected behavior. 
We're using OpenJPA 1.2.1 together with Oracle (not sure which version but 
I can find out). Switching OpenJPA versions are not really an option at 
this point.

As a sidenote: the "AND 1 = 1"-part is kinda funny to. Not sure what 
causes this behavior or what it's supposed to add. 

Grateful for any hints you may have for me.

Best regards
Magnus Andersson

Reply via email to