我有一个表“ questions”,其中包含字段ID,版本以及其他字段。一个问题可以有多个具有相同ID和不同版本的记录。我需要为每个问题的最高版本选择问题记录。
我的SQL查询是
select * from questions v1 inner join
(select id, max(version) as highest_version from
questions group by id
)as v2 on v1.id = v2.id and v1.version = v2.highest_version;
此查询可从Sequel pro进行。但是我需要从Java运行此程序,而我正在使用休眠模式。
我的Java代码是:
String assertQuestionQuery = "select v1 from Question v1 inner join "
+ "(select t.id, max(t.version) as highest_version "
+ "from Question t "
+ "group by t.id) "
+ "as v2 on v1.id = v2.id and v1.version = v2.highest_version";
Query q = sourceEm.createQuery(assertQuestionQuery, Question.class);
List<Question> questionVersions = q.getResultList();
我收到以下错误:
ERROR org.hibernate.hql.internal.ast.ErrorCounter line 1:87: unexpected token: (
如果删除括号,则会出现以下错误:
ERROR org.hibernate.hql.internal.ast.ErrorCounter line 1:87: unexpected token: select
请尝试以下更改。
createNativeQuery(..)
代替createQuery(..)
select v1.* from...
而不是select v1 from...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句