【问题标题】:Getting Hibernate exception: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path获取休眠异常:org.hibernate.hql.internal.ast.QuerySyntaxException:路径无效
【发布时间】:2020-12-02 03:40:32
【问题描述】:

使用弹簧靴 2.0。我有一个类,其中有一个命名查询试图从常量类中获取值;

package com.abc.xyz.ddd.pkg;
@NamedQueries({       
        @NamedQuery(name = "Test.findbyAge", query = "select test from Test test" +
                " where test.name in (:nameList) and nvl(test.id, 0) <= com.abc.xyz.ddd.Constants.ID"))
})
public class Test{
    
    
}   

常量类为:

package com.abc.xyz.ddd;    
public class Constants {

    public final static int ID = 100;
    
}

错误: 引起:org.hibernate.HibernateException:命名查询中的错误: Test.findbyAge 失败,因为:org.hibernate.hql.internal.ast.QuerySyntaxException:无效路径:'com.abc.xyz.ddd.Constants.ID'

【问题讨论】:

  • 您不能在 JPQL/HQL 查询中显式使用 java 类常量。您应该将其作为查询参数传递
  • 任何例子,我可以看看
  • 这里的nvl() 是什么?
  • nvl() 是空检查功能。看这里:forum.hibernate.org/viewtopic.php?f=1&t=971983
  • 您可以像@NamedQuery(name = "Test.findbyAge", query = "select test from Test test where test.name in (:nameList) and COALESCE(test.id, 0) &lt;= "+ com.abc.xyz.ddd.Constants.ID) 一样做,并确保Constants.ID 是字符串或将其转换为字符串

标签: spring-boot hibernate hql


【解决方案1】:

我建议你用这种方式重写你的查询:

@NamedQueries({
    @NamedQuery(
       name = "Test.findbyAge",
       query = "select test from Test test where test.name in (:nameList) and nvl(test.id, 0) <= :constantsID"
    )
})
public class Test {
}

然后就这样传递com.abc.xyz.ddd.Constants.ID

entityManager
  .createNamedQuery("Test.findbyAge", Test.class)
  .setParameter("constantsID", com.abc.xyz.ddd.Constants.ID)
  ...

【讨论】:

    猜你喜欢
    • 2018-08-11
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2014-03-25
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多