由于意外的AST节点,使用JPA TREAT的命名查询无法正常工作

时间:2019-05-02 11:19:32

标签: hibernate jpa jpql named-query

我正在尝试创建一个命名查询,该查询给出了错误。

" SELECT p FROM Project p  " +
" JOIN FETCH p.employees e " +
" WHERE p.id=:id AND ( TREAT(e AS FullTimeEmployee).code = :code OR e.state=:state)"

项目类

public class Project {
    @Id
    @GeneratedValue
    private long id;
    private String code;
    @OneToMany(cascade = CascadeType.ALL)
    private List<Employee> employees;

现在Employee类已经扩展了多个类。命名查询的目的是通过过滤后的雇员(具有特定状态)或FullTimeEmployee(具有特定代码)来获取一个项目。

错误:

<May 2, 2019 12:22:21,205 PM CEST> <Error> <org.hibernate.hql.internal.ast.ErrorCounter> <BEA-000000> <<AST>:1:127: unexpected AST node: (
<AST>:1:127: unexpected AST node: (
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:4936)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExprLhs(HqlSqlBaseWalker.java:5370)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:4831)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1263)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4552)
        Truncated. see log file for complete stacktrace

1 个答案:

答案 0 :(得分:0)

JPA 2.1中引入了TREAT函数。

Hibernate支持版本4.3或5.0的JPA 2.1。

因此,要使用TREAT,您必须更新Hibernate。