I have created the following query for PostgreSQL which is working fine in SqlWorkbench. But when I integrated this in Java code, I'm getting a syntax error exception:
Query running fine in SqlWorkbench:
ELECT poaiF.fnsku,
poaiF.acknowledgement_type_code AS last_ack_code
FROM po_acknowledgement_items poaiF,
(SELECT Max(poa.po_acknowledgement_id) AS last_po_ack_id,
poai.fnsku
FROM po_acknowledgement_items poai,
po_acknowledgements poa
WHERE poa.po_acknowledgement_id = poai.po_acknowledgement_id
AND poa.order_id = '5D7P2FLB'
GROUP BY poai.fnsku) t
WHERE t.last_po_ack_id = poaiF.po_acknowledgement_id
AND t.fnsku = poaiF.fnsku
Query in java (giving syntax error in beginning of (SELECT Max(poa.po_acknowledgement_id) AS last_po_ack_id):
private static final String LAST_ACK_CODE_QUERY1 = "SELECT poaiF.fnsku, \n" +
" poaiF.acknowledgement_type_code AS last_ack_code \n" +
"FROM po_acknowledgement_items poaiF, \n" +
" (SELECT Max(poa.po_acknowledgement_id) AS last_po_ack_id, \n" +
" poai.fnsku \n" +
" FROM po_acknowledgement_items poai, \n" +
" po_acknowledgements poa \n" +
" WHERE poa.po_acknowledgement_id = poai.po_acknowledgement_id \n" +
" AND poa.order_id = \'5D7P2FLB\' \n" +
" GROUP BY poai.fnsku) t \n" +
"WHERE t.last_po_ack_id = poaiF.po_acknowledgement_id \n" +
" AND t.fnsku = poaiF.fnsku";
While running the query through the integration test, getting the following error:
unexpected token: ( near line 4, column 8 which is the beginning of (SELECT Max(poa.po_acknowledgement_id)
I am running through org.springframework.orm.hibernate3.LocalSessionFactoryBean which is as follows.
SessionFactory postgresSessionFactory; //initialized with org.springframework.orm.hibernate3.LocalSessionFactoryBean class.
final Session session = postgresSessionFactory.getCurrentSession();
String query = LAST_ACK_CODE_QUERY;
final Query selectQuery = session.createQuery(query).setTimeout(queryTimeout);
final List<Object[]> terms = selectQuery.list();
Please help me to resolve this issue.
\'?JOINsyntax. Easier to write (without errors), easier to read and maintain, and easier to convert to outer join if needed!