【问题标题】:Unexpected end of subtree with Criteria Query using isEmpty使用 isEmpty 进行条件查询的子树意外结束
【发布时间】:2019-12-25 09:08:56
【问题描述】:

我在使用 Criteria API 开发 Spring Boot 应用程序时遇到了问题。

我有一个简单的Employer 实体,其中包含一组Job.ID(不是实体,它们在需要时使用存储库拉出)。 EmployerJob 是多对多关系。此映射仅用于查找没有工作的员工。

public class Employer {

    @ElementCollection
    @CollectionTable(
        name = "EMPLOYEE_JOBS"
        joinColumns = @JoinColumn(name = "EMP_ID")
    @Column(name = "JOB_ID")
    private final Set<String> jobs = new HashSet<>(); //list of ids of jobs for an employee 
}

然后我有一个通用函数,它通过给定的属性路径和任何IEntity 实现的命令返回谓词(规范)。

public <E extends IEntity> Specification<E> createPredicate(String attributePath, String command) {
    return (r, q, b) -> {
        Path<?> currentPath = r;
        for(String attr : attributePath.split("\\.")) {
            currentPath = currentPath.get(attr);
        }

        if(Collection.class.isAssignableFrom(currentPath.getJavaType())) {
            //currentPath points to PluralAttribute
            if(command.equalsIgnoreCase("empty")) {
                return b.isEmpty((Expression<Collection<?>>)currentPath);
            }
        }

    }
}

如果想获取当前没有工作的所有员工的列表,我希望我可以创建如下谓词:

Specification<Employer> spec = createPredicate("jobs", "empty");

//or if I want only `Work`s whose were done by employer with no job at this moment
Specification<Work> spec = createPredicate("employerFinished.jobs", "empty");

不幸的是,这不起作用并引发以下异常:

org.hibernate.hql.internal.ast.QuerySyntaxException: 
unexpected end of subtree 
[select generatedAlias0 from Employer as generatedAlias0 
where generatedAlias0.jobs is empty]

是否有解决方法如何使这项工作?

【问题讨论】:

    标签: spring hibernate oracle11g criteria


    【解决方案1】:

    Hibernate 中的这个错误自 2011 年 9 月以来就已为人所知,但遗憾的是尚未修复。 (更新:此错误自 5.4.11 起已修复)

    https://hibernate.atlassian.net/browse/HHH-6686

    幸运的是,有一个非常简单的解决方法,而不是:

    "where generatedAlias0.jobs is empty" 
    

    你可以使用

    "where size(generatedAlias0.jobs) = 0"
    

    这样查询将按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 2010-12-27
      • 1970-01-01
      • 2011-01-05
      • 2013-10-23
      • 1970-01-01
      相关资源
      最近更新 更多