简体   繁体   中英

Mysql query runs perfectly but JPA query returns nothing

I am trying to get this query to work in JPA

It works perfectly in mysql:

select * from order_header join order_detail where (order_header.buying_partner_id = 1 or order_header.buying_partner_id = 3) AND ((order_detail.warehouse_id = 6) or (order_header.warehouse_id = 6 and order_detail.warehouse_id is null ) ) and (order_detail.tracking_number is null or order_detail.tracking_number = '') and order_detail.canceled != 1 and (order_header.order_num is NOT NULL and order_header.order_num != '') and order_header.timestamp > '2017-01-24 08:33:00.096' group by order_header.order_num order by order_header.comment DESC;

Results are returned.

JPA Query:

Select h FROM OrderHeader h JOIN h.orderDetails d WHERE  ( h.buyingPartner.id = 1 or h.buyingPartner.id = 3 ) AND  (    (d.warehouse.id =6)         OR     ( h.warehouse.id =6  AND d.warehouse.id IS null  )   )     AND (d.trackingNumber IS null or d.trackingNumber = '')     AND  d.canceled != 1 AND (h.orderNum IS NOT null AND h.orderNum != '') and h.timestamp > '2017-01-24 09:32:39.865'  GROUP BY h.orderNum ORDER BY h.comment DESC

It returns nothing. No errors. Just returns nothing.

These are the exact same statements.

There must be something wrong with my JPA statement.

Select h FROM OrderHeader h JOIN h.orderDetails d WHERE ( h.buyingPartner.id = 1 or h.buyingPartner.id = 3 ) AND ( (d.warehouse.id =6) OR ( h.warehouse.id =6 AND d.warehouse.id IS null ) ) AND (d.trackingNumber IS null or d.trackingNumber = '') AND d.canceled != 1 AND (h.orderNum IS NOT null AND h.orderNum != '') and h.timestamp > '2017-01-24 09:32:39.865' GROUP BY h.orderNum ORDER BY h.comment DESC

JPA Query uses the JOIN condition when you created the relationship between entities.

For raw SQL query you have not provided that ON condition which will be included default in JPA Query.

That's the only difference I see in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM