Hibernate谓词 - 路径无效

时间:2018-05-28 08:57:34

标签: java hibernate jpa java-ee criteria-api

让我们有三个实体:Event,MessageEvent和Message。 MessageEvent是Event的子类(正确设置了DiscriminatorValue)。 MessageEvent有一个附加属性" message",它是Message类型。它的定义如下:

@ManyToOne(optional = false, cascade = CascadeType.REMOVE)
@JoinColumn(name = "MESAGE_ID", nullable = false)
private Message message;

现在,我们正在尝试设置一个谓词,该谓词将找到其消息的id为2的所有MessageEvent。我们按以下方式执行:

cb.equal(cb.treat(root, MessageEvent.class).join(MessageEvent_.message, JoinType.LEFT).get(Message_.id), 2)

代码编译得很好,但是,我们在运行时遇到这个错误:

5:40:59,891 ERROR [org.hibernate.hql.internal.ast.ErrorCounter] (default task-2)  Invalid path: 'generatedAlias1.id':  Invalid path: 'generatedAlias1.id'
at org.hibernate//org.hibernate.hql.internal.ast.util.LiteralProcessor.lookupConstant(LiteralProcessor.java:114)
at org.hibernate//org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:214)
at org.hibernate//org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:109)
at org.hibernate//org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:104)
at org.hibernate//org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:1033)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1286)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4699)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4169)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2134)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2084)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2084)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2084)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2084)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2059)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2059)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:813)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:607)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:311)
at org.hibernate//org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:259)

Hibernate显然在构造相应的SQL查询时遇到了问题。问题是在Hibernate本身还是我们做错了什么?

https://github.com/JurajMlich/hibernate-bug-demonstration提供最小的演示。

在Java 8,Java 10(openjdk),Wildfly 10,Wildfly 12上测试。使用eclipse链接ORM时正确处理相同的查询。

1 个答案:

答案 0 :(得分:1)

问题出在Hibernate的引擎盖下。我甚至在他们的错误跟踪系统中发现了这个错误,并发现它已被修复 - https://hibernate.atlassian.net/browse/HHH-10561。然而,Wildfly 12使用5.1.x.将Hibernate升级到5.2.17后,问题就消失了。