1

I am getting a strange error when trying to execute a native query in hibernate this is my query:

String qsf = "LEFT OUTER JOIN record_v2_record_audit rra ON r.record_id = rra.record_id "
    + "LEFT OUTER JOIN record_audit ra ON rra.record_audit_id = ra.record_audit_id AND rra.record_audit_id = ra.record_audit_id "
    + "LEFT OUTER JOIN repeating_field rf ON r.record_id = rf.record_id "
    + "LEFT OUTER JOIN repeating_field_field rff ON rf.repeating_field_id = rff.repeating_field_id "
    + "LEFT OUTER JOIN field_v2 f ON rff.field_id = f.field_id AND rff.field_id = f.field_id "
    + "LEFT OUTER JOIN field_image_v2 fi ON f.field_id = fi.field_id "
    + "WHERE r.record_type_id = 1 " 
    + "AND r.public_access = 0 "
    + "AND r.status = 'A' " 
    + "AND ra.process_type = 'A' "
    + "AND rf.field_type_id = 100032011 "
    + "AND fi.public_access = 'A'";

    qsc = "SELECT COUNT(DISTINCT r.record_id) FROM record_v2 r "
                    + qsf + " ;";

And I execute the query thuswise:

BigInteger totalRecords = (BigInteger) entityManager.createNativeQuery(
                qsc).getSingleResult();

But I am getting this error:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query

and the bit that is really throwing me is this:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit 2' at line 1

appearing in the stacktrace.

The query executes without error from MySQl admin so I am at a loss here.

I am using JBoss 5 with JDBC 5.1.15 in Ubuntu 10.10.

Thanks in advance for any help...!

travega
  • 8,284
  • 16
  • 63
  • 91

1 Answers1

2

I guess the problem is caused by the trailing semicolon, try to remove it:

qsc = "SELECT COUNT(DISTINCT r.record_id) FROM record_v2 r " + qsf; 
axtavt
  • 239,438
  • 41
  • 511
  • 482