【发布时间】:2019-12-25 09:08:56
【问题描述】:
我在使用 Criteria API 开发 Spring Boot 应用程序时遇到了问题。
我有一个简单的Employer 实体,其中包含一组Job.ID(不是实体,它们在需要时使用存储库拉出)。 Employer 和 Job 是多对多关系。此映射仅用于查找没有工作的员工。
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