问题描述
使用Spring Boot 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”
解决方法
我建议您以这种方式重写查询:
@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)
...