简体   繁体   English

JPA Criteria查询双重连接并从句休眠错误

[英]JPA Criteria query double joins and hibernate error with-clause

Based on this answer https://stackoverflow.com/a/2111420/3989524 I first created a working SQL: 基于这个答案https://stackoverflow.com/a/2111420/3989524我首先创建了一个有效的SQL:

SELECT d.*
FROM device d
LEFT OUTER JOIN installation_record ir1 ON (d.id = ir1.device)
LEFT OUTER JOIN installation_record ir2 ON (d.id = ir2.device AND ir1.install_date < ir2.install_date)
WHERE ir2.id IS NULL AND ir1.uninstall_date IS NULL;

and then a criteria query which looks to produce an equivalent HQL (in the end), but Hibernate throws an error: 然后是一个标准查询,看起来会产生一个等效的HQL(最后),但是Hibernate抛出错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: with-clause referenced two different from-clause elements org.hibernate.hql.internal.ast.QuerySyntaxException:带子句引用了两个不同的从子句元素

The HQL from the error message: 错误消息中的HQL:

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 )

The criteria query: 条件查询:

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();

Is there anyway to get what I want or there no other way around some internal hibernate bug. 无论如何,有没有想要的东西,或者没有其他解决内部休眠错误的方法。

Maybe: 也许:

final Join<InstallationRecord, InstallationRecord> join2 = root.join(InstallationRecord_.id, JoinType.LEFT);

because ON (d.id = ir2.device AND is missing in the HQL. 因为ON (d.id = ir2.device AND并且在HQL中丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM