我在使用JPQL执行简单更新查询时遇到ClassCastException。
以下是异常追踪:
Caused by: java.lang.ClassCastException: org.hibernate.hql.internal.ast.tree.IdentNode cannot be cast to org.hibernate.hql.internal.ast.tree.DotNode
at org.hibernate.hql.internal.ast.tree.AssignmentSpecification.<init>(AssignmentSpecification.java:71) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.hql.internal.ast.HqlSqlWalker.evaluateAssignment(HqlSqlWalker.java:1133) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.hql.internal.ast.HqlSqlWalker.evaluateAssignment(HqlSqlWalker.java:1127) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.assignment(HqlSqlBaseWalker.java:1042) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.setClause(HqlSqlBaseWalker.java:747) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.updateStatement(HqlSqlBaseWalker.java:367) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:255) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1777) [hibernate-core-4.2.0.SP1.jar:4.2.0.SP1]
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291) [hibernate-entitymanager-4.2.0.SP1.jar:4.2.0.SP1]
at org.jboss.as.jpa.container.AbstractEntityManager.createQuery(AbstractEntityManager.java:425) [jboss-as-jpa-7.2.1.Final-redhat.jar:7.2.1.Final-redhat]
我正在执行的查询是从工作池中移动任务:
public Boolean changeWorkpool(final TaskWorkpoolChangeRequest taskWorkpoolChangeRequest) {
final Query query = entityManager.createQuery("UPDATE Task SET workpoolId = :workpoolId"
+ " WHERE taskId= :taskId");
query.setParameter("workpoolId", taskWorkpoolChangeRequest.getWorkpoolId())
.setParameter("taskId", taskWorkpoolChangeRequest.getTaskId());
return BooleanUtils.toBoolean(query.executeUpdate());
}
有人可以就这个例外的原因提出一些意见。
提前致谢。
答案 0 :(得分:0)
我最近几天遇到过同样的问题而且我对JPQL进行了一些挖掘,发现JPQL更新查询无法更新关系。
正如https://docs.oracle.com/middleware/1212/toplink/OTLCG/queries.htm#OTLCG94000
中所述更新查询只能更新对象或其embeddables的属性,其关系无法更新。
我注意到Workpool
和Task
可能有关系。所以也许它会导致问题。
希望它有所帮助。