基于这个答案http://octave.1599824.n4.nabble.com/quot-features-quot-problems-td4675361.html我首先创建了一个有效的SQL:
SELECT generatedAlias0 FROM Device AS generatedAlias0
LEFT JOIN generatedAlias0.installationRecordList AS generatedAlias1
LEFT JOIN generatedAlias0.installationRecordList AS generatedAlias2
WITH generatedAlias1.installDate<generatedAlias2.installDate
WHERE ( generatedAlias2.id IS NULL ) AND ( generatedAlias1.uninstallDate IS NULL )
然后是一个标准查询,它看起来会生成一个等价的HQL(最后),但是Hibernate会抛出一个错误:
org.hibernate.hql.internal.ast.QuerySyntaxException:with-clause引用了两个不同的from-clause元素
错误消息中的HQL:
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Device> cq = cb.createQuery(Device.class);
final Root<Device> root = cq.from(Device.class);
final Join<Device, InstallationRecord> join1 = root.join(Device_.installationRecordList, JoinType.LEFT);
final Join<Device, InstallationRecord> join2 = root.join(Device_.installationRecordList, JoinType.LEFT);
join2.on(cb.lessThan(join1.get(InstallationRecord_.installDate), join2.get(InstallationRecord_.installDate)));
cq.select(root);
final List<Predicate> predicates = Lists.newArrayList();
predicates.add(cb.isNull(join2.get(InstallationRecord_.id)));
predicates.add(cb.isNull(join1.get(InstallationRecord_.uninstallDate)));
cq.where(predicates.toArray(new Predicate[] {}));
return em.createQuery(cq).getResultList();
标准查询:
{{1}}
无论如何都要得到我想要的东西,或者没有别的办法解决一些内部的hibernate错误。
答案 0 :(得分:-1)
也许:
final Join<InstallationRecord, InstallationRecord> join2 = root.join(InstallationRecord_.id, JoinType.LEFT);
因为HQL中缺少ON (d.id = ir2.device AND
。