title: Hibernate Native Query error
body: <p>I am getting a strange error when trying to execute a native query in hibernate this is my query:</p>
  
  <pre><code>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 + " ;";
  </code></pre>
  
  <p>And I execute the query thuswise:</p>
  
  <pre><code>BigInteger totalRecords = (BigInteger) entityManager.createNativeQuery(
                  qsc).getSingleResult();
  </code></pre>
  
  <p>But I am getting this error:</p>
  
  <blockquote>
    <p>javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query</p>
  </blockquote>
  
  <p>and the bit that is really throwing me is this:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>appearing in the stacktrace.</p>
  
  <p>The query executes without error from MySQl admin so I am at a loss here.</p>
  
  <p>I am using JBoss 5 with JDBC 5.1.15 in Ubuntu 10.10.</p>
  
  <p>Thanks in advance for any help...!</p>