HIbernate HQL 意外令牌
HIbernate HQL unexpected token
我有以下 Hibernate 查询:
String HSQL = "from Page as p "
+ "where not exists (from Link as l where l.dealId = p.id) "
+ "and p.userId = :userId";
return (List<Deal>) sessionFactory.getCurrentSession()
.createQuery(HSQL)
.setParameter("userId", userId)
.list();
但是在方法 .createQuery(HSQL)
上失败并出现以下异常
[ ERROR ] [12.11.2019 13:19:10] --- U: I: S: --- org.hibernate.hql.internal.ast.ErrorTracker: line 1:137: unexpected token: .
[ ERROR ] [12.11.2019 13:19:10] --- U: I: S: --- org.hibernate.hql.internal.ast.ErrorTracker: line 1:137: unexpected token: .
antlr.NoViableAltException: unexpected token: .
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClassOrOuterQueryPath(HqlBaseParser.java:1972) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1499) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1343) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1063) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:748) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.subQuery(HqlBaseParser.java:3910) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.quantifiedExpression(HqlBaseParser.java:3515) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3373) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3273) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
...
我的 Hibernate 版本是 5.4.1。
查询有什么问题?
我认为您的实体 dealId
属于 Page
类型而不是 Long
类型,因此您可以使用 :
"where not exists (from Link as l where l.dealId = p) "
^^^^^^^^^^^^
我有以下 Hibernate 查询:
String HSQL = "from Page as p "
+ "where not exists (from Link as l where l.dealId = p.id) "
+ "and p.userId = :userId";
return (List<Deal>) sessionFactory.getCurrentSession()
.createQuery(HSQL)
.setParameter("userId", userId)
.list();
但是在方法 .createQuery(HSQL)
上失败并出现以下异常
[ ERROR ] [12.11.2019 13:19:10] --- U: I: S: --- org.hibernate.hql.internal.ast.ErrorTracker: line 1:137: unexpected token: .
[ ERROR ] [12.11.2019 13:19:10] --- U: I: S: --- org.hibernate.hql.internal.ast.ErrorTracker: line 1:137: unexpected token: .
antlr.NoViableAltException: unexpected token: .
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClassOrOuterQueryPath(HqlBaseParser.java:1972) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1499) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1343) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1063) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:748) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.subQuery(HqlBaseParser.java:3910) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.quantifiedExpression(HqlBaseParser.java:3515) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3373) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3273) [hibernate-core-5.4.1.Final.jar:5.4.1.Final]
...
我的 Hibernate 版本是 5.4.1。
查询有什么问题?
我认为您的实体 dealId
属于 Page
类型而不是 Long
类型,因此您可以使用 :
"where not exists (from Link as l where l.dealId = p) "
^^^^^^^^^^^^