【问题标题】:Hibernate: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 6...)休眠:org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌:(靠近第 1 行,第 6 列...)
【发布时间】:2015-11-14 12:16:01
【问题描述】:

我声明我在 Mac OSX 上使用 Hibernate、MySQL。

我阅读了与我的问题有关的帖子,但不幸的是我无法解决它。下面放置有问题的代码:

public List<FeedMessage> getLatestFeedMessage() {
    List<FeedMessage> messages = new ArrayList<>();

    String hql = "from (select nomeFeed, title, max(pubDate) as maxdate "
                  + "from FeedMessage group by nomeFeed) as x inner join FeedMessage as f "
                                    + "on f.nomeFeed = x.nomeFeed and f.pubDate = x.maxdate";


    System.out.println(hql);
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery(hql);
        messages=query.list();
    } catch (QueryException e) {
        e.printStackTrace();
    } finally {
        if(session.isOpen())
        session.close();
    }

    return messages;

}

我正在尝试从 MySQL 数据库中选择每个组的第一行,但出现以下错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 6 [from (select nomeFeed, title, max(pubDate) as maxdate from it.unirc.fantapjam.FeedMessage.Model.FeedMessage group by nomeFeed) as x inner join FeedMessage as f on f.nomeFeed = x.nomeFeed and f.pubDate = x.maxdate]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
    at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)...........

我认为问题在于 Hibernate 无法翻译查询,因为如果我直接运行到 MySQL,它就可以工作。或者,我尝试运行后续查询:

String hql = "from (select nomeFeed, title, max(pubDate) as maxdate "
                                    + "from FeedMessage "
                                    + "group by nomeFeed) as x,  FeedMessage as f "
                    + "where f.nomeFeed = x.nomeFeed and f.pubDate = x.maxdate";

但我得到了相同的结果。

【问题讨论】:

  • 你尝试执行SQL还是HQL?..
  • 也许你应该看看方法Session.createSQLQuery(String)而不是Session.createQuery(String)
  • 我都试过了,但效果不一样。
  • 你能分享第二个错误和堆栈跟踪吗?
  • 请注意,SQL 查询应以SELECT 子句开头。

标签: java mysql sql hibernate hql


【解决方案1】:

很遗憾,您无法在 HQL 中执行此操作。根据休眠文档https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries

请注意,HQL 子查询只能出现在 select 或 where 条款。

而您的子查询位于 from 子句中。如果您希望在 from 语句中使用子查询,则必须使用本机 SQL,如下所述:https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-12
    • 2016-02-21
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多