Hibernate 本机查询错误

发布于 2024-11-02 22:10:07 字数 1576 浏览 2 评论 0原文

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

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”附近使用的正确语法

在堆栈跟踪中出现的第 1 行“limit 2”附近使用的正确语法。

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

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

预先感谢您的任何帮助...!

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...!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-11-09 22:10:07

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

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

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; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文