Hibernate Native Query错误

时间:2011-04-21 05:06:13

标签: mysql hibernate nativequery

我在hibernate中尝试执行本机查询时遇到一个奇怪的错误,这是我的查询:

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 + " ;";

然后我执行查询:

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

但是我收到了这个错误:

  

javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法执行查询

真正抛弃我的是这个:

  

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“limit 2”附近使用正确的语法

出现在堆栈跟踪中。

查询执行时没有来自MySQl管理员的错误,所以我在这里不知所措。

我在Ubuntu 10.10中使用JBoss 5和JDBC 5.1.15。

提前感谢您的帮助......!

1 个答案:

答案 0 :(得分:1)

我猜这个问题是由尾随的分号引起的,试着删除它:

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