gpt4 book ai didi

java - Hibernate:org.hibernate.hql.internal.ast.QuerySyntaxException:意外标记:(第 1 行附近,第 6 列...)

转载 作者:行者123 更新时间:2023-11-28 23:53:52 24 4
gpt4 key购买 nike

我声明我在 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";

但我得到了相同的结果。

最佳答案

不幸的是,您不能在 HQL 中执行此操作。根据 hibernate 文档 https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries

Note that HQL subqueries can occur only in the select or where clauses.

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

关于java - Hibernate:org.hibernate.hql.internal.ast.QuerySyntaxException:意外标记:(第 1 行附近,第 6 列...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32125285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com