1

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

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 行的“限制 2”附近使用正确的语法

出现在堆栈跟踪中。

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

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

在此先感谢您的帮助...!

4

1 回答 1

2

我猜问题是由尾随分号引起的,尝试将其删除:

qsc = "SELECT COUNT(DISTINCT r.record_id) FROM record_v2 r " + qsf; 
于 2011-04-21T07:53:20.337 回答