我正在尝试使用带有两个具有双向关系的实体的规范创建动态查询。实体是:
@Entity
@Table("SUPPLIERS")
public class Supplier implements Serializable {
@Id
Column("ID")
private Long id;
@Id
Column("COMPANY_ID")
private Long companyId;
}
@Entity
@Table("EMPLOYEES")
public class Employee implements Serializable {
@Id
private Long id;
@ManyToOne
@JoinColumns({
@JoinColumn(name = "FIRM_ID", referencedColumnName = "ID"),
@JoinColumn(name = "FIRM_COMPANY_ID", referencedColumnName = "COMPANY_ID")
})
private Supplier supplier;
}
当我想根据供应商选择员工时,
return new Specification<Employee>() {
@Override
public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Long[] supplierCodes = {1L, 2L};
Subquery<Supplier> supplierBasicSubquery = query.subquery(Supplier.class);
Root<Supplier> supplierBasicRoot = supplierBasicSubquery.from(Supplier.class);
Join<Employee, Supplier> sqTfV = root.join("supplier", JoinType.INNER);
supplierBasicSubquery.select(sqTfV).where(sqTfV.<Long>get("id").in(supplierCodes));
return root.<Supplier>get("supplier").in(supplierBasicSubquery);
}
};
执行时,它会生成如下的SQL:
SELECT ....
FROM EMPLOYEES E
INNER JOIN ....
WHERE (E.FIRM_ID, E.FIRM_COMPANY_ID) in
(SELECT (s.ID, s.COMPANY_ID) FROM SUPPLIERS WHERE SUPPLIER.ID in (1, 2))
如您所见,内部选择列被括号括起,导致Oracle抛出异常:
java.sql.SQLSyntaxErrorException: ORA-00920: invalid relational operator
我如何解决这个问题,有什么建议?
非常感谢。